[英]How to convert Graphics to bitmap and save it in c#?
currently I am learning to draw barcode using graphics method and dispose it as bitmap.目前我正在学习使用图形方法绘制条形码并将其处理为位图。 But when I going to save it, although it cans save successfully, but the result is black image, nothing can be seen from that.
但是当我要保存它时,虽然可以保存成功,但结果是黑色图像,从那里看不到任何东西。 What is the problem going on?
发生了什么问题?
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();
}
And this is my save function这是我的保存功能
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();
}
}
I cant provide any picture to proof that as my reputation is not enough to do so.我无法提供任何图片来证明我的声誉还不够。 Sorry.
对不起。
To draw into a Bitmap
use this code:要绘制
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);
}
Or:或者:
private void buttonSave_Click(object sender, EventArgs e)
{
Save( (Bitmap) pictureBoxBarcode.Image);
}
I have copied your drawing code.我已经复制了你的绘图代码。
I don't know why you want to make it look gray..?我不知道你为什么想让它看起来是灰色的..?
I am assuming that ean13.DrawEan13Barcode
is a function that uses the passed-in Graphics
object to draw something onto the Bitmap
that is associated with the Graphics
object.我假设
ean13.DrawEan13Barcode
是一个函数,它使用传入的Graphics
对象在与Graphics
对象关联的Bitmap
上绘制一些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.