【问题标题】:How to convert Graphics to bitmap and save it in c#?如何将图形转换为位图并将其保存在 c# 中?
【发布时间】:2015-09-18 00:38:11
【问题描述】:

目前我正在学习使用图形方法绘制条形码并将其处理为位图。但是当我要保存的时候,虽然可以保存成功,但是结果是黑色的,什么都看不到。到底是什么问题?

    private void buttonDraw_Click(object sender, EventArgs e)
    {
        System.Drawing.Graphics g = this.pictureBoxBarcode.CreateGraphics();
        g.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.SystemColors.Control),
        new Rectangle(0, 0, pictureBoxBarcode.Width, pictureBoxBarcode.Height));
        ean13.DrawEan13Barcode(g, new System.Drawing.Point(0, 0));
        g.Dispose();

    }

这是我的保存功能

    public void Save(Bitmap original)
    {
        // Displays a SaveFileDialog so the user can save the Image
        // assigned to Button2.
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|Png Image|*.png";
        saveFileDialog1.Title = "Save an Image File";
        saveFileDialog1.ShowDialog();

        // If the file name is not an empty string open it for saving.
        if (saveFileDialog1.FileName != "")
        {
            // Saves the Image via a FileStream created by the OpenFile method.
            System.IO.FileStream fs =
               (System.IO.FileStream)saveFileDialog1.OpenFile();
            // Saves the Image in the appropriate ImageFormat based upon the
            // File type selected in the dialog box.
            // NOTE that the FilterIndex property is one-based.

            switch (saveFileDialog1.FilterIndex)
            {
                case 1:
                    original.Save(fs,
                       System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;

                case 2:
                    original.Save(fs,
                       System.Drawing.Imaging.ImageFormat.Bmp);
                    break;

                case 3:
                    original.Save(fs,
                       System.Drawing.Imaging.ImageFormat.Gif);
                    break;

                case 4:
                    original.Save(fs,
                       System.Drawing.Imaging.ImageFormat.Png);
                    break;
            }
            fs.Close();
        }
    }

我无法提供任何图片来证明我的声誉不足以证明这一点。对不起。

【问题讨论】:

  • 我怀疑有人可以在不看代码的情况下回答这个问题。
  • @SteveFerg 我想知道那个绘图区是什么?画布、位图或其他东西?
  • 你有十几种不同的选择,看看BitMap类定义:msdn.microsoft.com/en-us/library/…
  • 您有两个选择: 使用它的 Paint 事件和它的 e.Graphics 对象写入到 PictureBox 控件的表面。 或者您可以使用从它创建的图形对象绘制到位图中。 - 相反,你混合了两种方式,这是行不通的! - 有关这两个选项的更多信息,请参阅here..!

标签: c# graphics


【解决方案1】:

要绘制到Bitmap,请使用以下代码:

private void buttonDraw_Click(object sender, EventArgs e)
{
    Bitmap bmp = new Bitmap(pictureBoxBarcode.ClientSize.Width,
                            pictureBoxBarcode.ClientSize.Height);
    using (Graphics g = Graphics.FromImage(bmp))
    {
       g.Clear(SystemColors.Control);
       ean13.DrawEan13Barcode(g, new System.Drawing.Point(0, 0));
    }
    // display:
    pictureBoxBarcode.Image = bmp;
    // save:
    bmp.Save(yourfilename,  ImageFormat.Png);
    // ..or..: 
    Save(bmp);
}

或者:

private void buttonSave_Click(object sender, EventArgs e)
{
    Save( (Bitmap) pictureBoxBarcode.Image);
}

我已经复制了你的绘图代码。

我不知道你为什么要让它看起来是灰色的..?

我假设ean13.DrawEan13Barcode 是一个函数,它使用传入的Graphics 对象在与Graphics 对象关联的Bitmap 上绘制一些东西。

【讨论】:

  • 你的答案,经过这段代码后,我发现我之前的错误是我用图形而不是位图来做,对吗?
  • 看起来灰色是什么意思?
  • 1:您的代码是混合的。请注意,您始终使用 Graphics 对象,并且它始终绑定到 Btimap。如果在控件上绘图,则在 Paint 事件中创建 Graphics 对象,而 Bitmap 是控件的表面。这里我们绘制一个Bitmap,自己创建Graphics对象,可以在Image属性中显示Bitmap。 - 2:SystemColors.Control是一种灰色,在大多数机器上,
  • oo,请解释一下,我知道我错在哪里,我只是不想画出条形码,所以我不知道 SystemColors.Control 的用途,就像代码中一样,你已经清除了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多