如何保存图形pictrebox? [英] how to save graphical pictrebox?

查看:120
本文介绍了如何保存图形pictrebox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我有一个两个按钮(savebtn用于保存图片和drawbtn用于绘制线)和一个图片框来显示

我正在使用所有名称空间,例如System.Drawing & System.Drawing.Drawing2D



in drawbtn:



图形gra = pictureBox1.CreateGraphics(); 
gra.DrawLine( new Pen(Color.Red, 4 ), 0 0 100 100 );





i在savebtn写了简单的保存代码:

< pre lang =c#> pictureBox1.Image.Save( @ C:\ .png);





当我点击savebtn它不起作用并给我NullReferenceException unhandled运行时错误

谢谢

解决方案

自从我使用GDI +以来已经有好几年了,但是这里的代码在发布之前已经过测试了。我一直在想有更好的方法来做到这一点!



问题:在Button Click EventHandler中放置一个断点,执行你实际调用的代码Image.Save,并检查pictureBox1.Image的值:你会看到它为null。这是错误的来源。



并且,如果你使用PictureBox的Paint事件,并在上面绘图,然后保存PictureBox.Image,你'得到(通常)黑色图形。



你需要做的是在BitMap上绘制,然后,当PictureBox需要刷新时,你需要复制BitBox到PictureBox:

  private 位图pbBitMap; 

private void Form1_Load( object sender,EventArgs e)
{
pbBitMap = new 位图(pictureBox1.Width,pictureBox1.Height);

图形pbGraphics = Graphics.FromImage(pbBitMap);

刷pbBrush = SolidBrush(pictureBox1.BackColor);

// 将BitMap的BackColor设置为PictureBox的BackColor
pbGraphics.FillRegion(pbBrush, new Region(pictureBox1.ClientRectangle));

pbGraphics.DrawLine( new Pen(Color.Red, 4 ), 0 0 100 100 );
}

private void pictureBox1_Paint( object sender,PaintEventArgs e)
{
e.Graphics.DrawImageUnscaled(pbBitMap, 0 0 );
}

private void btnSave_Click( object sender,EventArgs e)
{
pbBitMap.Save( @ C:\ Users \YourUserName \Desktop\Picture1.jpg,ImageFormat.Jpeg);
}


很好。你正试图保存一个里面没有任何东西的图像。



在PictureBox的SURFACE上绘图并不会把你画的内容放到图像中(位图) 。您正在绘制屏幕,​​而不是位图。


hi I have a two buttons(savebtn for save picture and drawbtn for draw line) and a picture box to show
I'm using all namespace such as "System.Drawing" & "System.Drawing.Drawing2D"

in drawbtn :

Graphics gra = pictureBox1.CreateGraphics();
gra.DrawLine(new Pen(Color.Red, 4), 0, 0, 100, 100);



i wrote simple save code in savebtn:

pictureBox1.Image.Save(@"C:\1.png");



when I click on savebtn it doesn't work and give me "NullReferenceException unhandled" run time error
thanks

解决方案

It's been years since I worked with GDI+, but the code here was tested before posting. I keep thinking there's a better way to do this, though !

The problem: put a break-point in the Button Click EventHandler that executes the code where you actually call Image.Save, and inspect the value of pictureBox1.Image: you'll see it's null. That's the source of the error.

And, if you use the Paint Event of a PictureBox, and draw on it, and then save the PictureBox.Image, you'll get (usually) a black graphic.

What you need to do is draw on a BitMap, and then, when the PictureBox needs refreshing, you need to copy the BitMap to the PictureBox:

private Bitmap pbBitMap;

private void Form1_Load(object sender, EventArgs e)
{
    pbBitMap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    
    Graphics pbGraphics = Graphics.FromImage(pbBitMap);
    
    Brush pbBrush = new SolidBrush(pictureBox1.BackColor);
    
    // set the BackColor of the BitMap to the BackColor of the PictureBox
    pbGraphics.FillRegion(pbBrush, new Region(pictureBox1.ClientRectangle));
    
    pbGraphics.DrawLine(new Pen(Color.Red, 4), 0, 0, 100, 100);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImageUnscaled(pbBitMap, 0, 0);
}

private void btnSave_Click(object sender, EventArgs e)
{
    pbBitMap.Save(@"C:\Users\YourUserName\Desktop\Picture1.jpg", ImageFormat.Jpeg);
}


Nice. You're trying to save an image that has nothing in it.

Drawing on the SURFACE of the PictureBox does not put what you are drawing into the image (bitmap). You are drawing to the screen, not the bitmap.


这篇关于如何保存图形pictrebox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆