绘制透明图像

发布于 2024-08-12 14:51:17 字数 2142 浏览 5 评论 0原文

我正在用 C# 编写 CSS 精灵引擎,但是遇到了一些问题。我创建主图像,设置所有属性,然后迭代精灵并将它们绘制到主图像。然而,当我保存主图像时,它似乎只是具有透明背景的空主图像,并且没有任何精灵。我很困惑我哪里出错了。

我正在使用的代码是:

    // 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 {

    }

I'm writing a CSS sprite engine in C#, however I'm having a few issues. I create the master image, set all the properties to that then iterate the sprites and draw those to the master image. However when I come to save the master image it only appears to be just the empty master image with transparent background and none of the sprites. I'm very confused at where I'm going wrong.

The code I'm using is:

    // 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 {

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云淡风轻 2024-08-19 14:51:17

什么是 sprite.Left 和 Top?如果它们不为 0,则可能会出现问题。我认为你的目标和来源是错误的?

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

如果您还没有尝试过更简单的 DrawImage 变体,请尝试一下。
例如 DrawImage(sprite.Sprite,0,0)

What are sprite.Left and Top? If they aren't 0 that may be a problem. I think you have dest and source the wrong way round?

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

Try a simpler DrawImage variant if you haven't already.
e.g. DrawImage(sprite.Sprite,0,0)

捎一片雪花 2024-08-19 14:51:17

您绘制“图形”,然后保存“位图”?我不确定该图形在内部是否与您原来的“位图”对象一起工作......

You draw to "graphics", but then then you save "bitmap"? I'm not sure if that graphics internally works with your original "bitmap" object...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文