2009-11-27 114 views
0

我正在用C#编写CSS sprite引擎,但是我遇到了一些问题。我创建主图像,设置所有属性,然后迭代sprites并将其绘制到主图像。但是,当我来保存主图像时,它看起来只是具有透明背景的空主图像,没有任何sprites。我对我出错的地方非常困惑。绘制透明图像

我正在使用的代码是:

// Work out the width/height required 
    int max_width = 0; 
    int max_height = 0; 

    foreach(SpriteInformation sprite in sprites) { 
     if (max_width < (sprite.Left + greatest_width)) max_width = sprite.Left + greatest_width; 
     if (max_height < (sprite.Top + greatest_height)) max_height = sprite.Top + greatest_height; 
    } 

    // Create new master bitmap 
    Bitmap bitmap = new Bitmap(max_width,max_height,PixelFormat.Format32bppArgb); 
    Graphics graphics = Graphics.FromImage(bitmap); 

    // Set background color 
    SolidBrush brush; 

    if (cbxBackground.Checked) { 
     if (txtColor.Text == "") { 
      brush = new SolidBrush(Color.Black); 
     } else { 
      brush = new SolidBrush(pnlColor.BackColor); 
     } 
    } else { 
     if (txtColor.Text == "") { 
      brush = new SolidBrush(Color.White); 
     } else { 
      brush = new SolidBrush(pnlColor.BackColor); 
     } 
    } 

    //graphics.FillRectangle(brush,0,0,bitmap.Width,bitmap.Height); 
    bitmap.MakeTransparent(brush.Color); 
    graphics.Clear(brush.Color); 

    // Copy images into place 
    ImageAttributes attr = new ImageAttributes(); 

    //attr.SetColorKey(brush.Color,brush.Color); 

    foreach(SpriteInformation sprite in sprites) { 
     Rectangle source = new Rectangle(0,0,sprite.Width,sprite.Height); 
     Rectangle dest = new Rectangle(sprite.Left,sprite.Top,sprite.Width,sprite.Height); 

     graphics.DrawImage(sprite.Sprite,dest,0,0,sprite.Width,sprite.Height,GraphicsUnit.Pixel,attr); 
    } 

    // Save image 
    string format = ddlFormat.Items[ddlFormat.SelectedIndex].ToString(); 

    if (format == "PNG") { 
     dlgSave.Filter = "PNG Images|*.png|All Files|*.*"; 
     dlgSave.DefaultExt = ",png"; 

     if (dlgSave.ShowDialog() == DialogResult.OK) { 
      bitmap.Save(dlgSave.FileName,ImageFormat.Png); 
     } 
    } else if (format == "JPEG") { 

    } else { 

    } 
+0

作为警告IE6不支持Alpha透明度的PNG,除非你使用8位PNG ....不幸的是System.Drawing不会让你以智能的方式输出8位PNG。 (选择正确的颜色并不聪明) – Caleb 2009-11-27 19:04:50

+0

我意识到IE6的限制,真的有点烦人。我看到了一些GIF颜色量化的东西,我可能也适用于此。 – Lloyd 2009-12-02 10:55:54

回答

0

什么是sprite.Left和Top?如果他们不是0可能是一个问题。我认为你的目标和方向是错误的?

http://msdn.microsoft.com/en-us/library/ms142045.aspx

尝试简单的DrawImage变种,如果你还没有准备好。 例如DrawImage(sprite.Sprite,0,0)

+0

sprite.Left/Top给出坐标在哪里绘图最终图像上的图像。我试过用DrawImage(sprite.Sprite,0,0)和DrawImage(sprite.Sprite,sprite.Left,sprite.Top)无济于事。我确定sprite.Sprite也被分配,因为它们只是从另一个PNG加载而未被触摸。 – Lloyd 2009-11-27 16:26:49

+0

:/我现在看到。在计算位图的最大尺寸时,为什么使用最大宽度/高度而不是实际的精灵尺寸?我只是在这里抓住吸管而已。我会尝试将所有这些偏移重置为0并渲染到大图像上,以确保将其渲染到您认为是目标图像的区域。然后你可以添加偏移回来。 – 2009-11-27 16:47:54

+0

我接下来会尝试。最大的宽度/高度基本上是精灵所需的最偏移+ n,这样我就知道图像有多大了。 – Lloyd 2009-11-27 16:50:15

-1

你画到“图形”,但随后然后保存“位图”?我不知道该图形是否与您的原始“位图”对象内部协同工作...

+0

这似乎确实是文档中的含义:http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx – 2009-11-27 16:14:28