如何将picturebox图像保存到jpg文件? [英] How to save picturebox image into jpg file?

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

问题描述

我有一个带圆圈和矩形的图片框,我将所有图片保存到jpg文件中,我试过了

pictureBox1.Image.Save (@" c:\\\jpg");

但我得到了错误

" System.NullReferenceException:对象引用未设置为<的实例br />
一个对象。


有人可以帮忙吗?谢谢......


-

通过 http://www.dotnetmonster.com

Hi, I have a picture box with circles and rectangles, and I wana save all
the images into a jpg file, i tried
pictureBox1.Image.Save(@"c:\1.jpg");
but I got and error
"System.NullReferenceException: Object reference not set to an instance of
an object."

Can anyone help? Thanks...

--
Message posted via http://www.dotnetmonster.com

推荐答案

嗨yaya,


你是直接在PictureBox上画画吗? (顺便说一句不是什么?

PictureBox的意思)。

你需要以某种方式在Bitmap上进行绘画,例如在你的
$中b $ b绘制程序在Bitmap上绘制而不是在最后使用

DrawImageUnscaled将结果绘制到某个控件上,或者将位图

放入PictureBox.Image属性。然后,您可以使用Image.Save(filename,

ImageFormat)将位图保存到文件。使用Graphics.FromImage()为你的绘画程序获得

a画布。


-

快乐编码!

Morten Wennevik [C#MVP]
Hi yaya,

Are you painting directly on a PictureBox? (which btw is not what a
PictureBox is meant for).
You need to somehow do the painting on a Bitmap, for instance when in your
paint procedure paint on the Bitmap instead and at the end use
DrawImageUnscaled to paint the result onto some control, or put the bitmap
into the PictureBox.Image property. You can then use Image.Save(filename,
ImageFormat) to save the Bitmap to file. Use Graphics.FromImage() to get
a paint canvas for your paint procedure.

--
Happy Coding!
Morten Wennevik [C# MVP]


我正在用绘图箱的绘画事件绘制形状,

图形g = e.Graphics;

g.DrawLine(myPen,new Point(xStart,yStart),new Point(xEnd,yStart));
I''m drawing the shapes with the paint event of the picturebox,
Graphics g = e.Graphics;
g.DrawLine(myPen,new Point(xStart,yStart),new Point(xEnd,yStart));


创建一个绘图板大小或您喜欢的任何尺寸的位图。


位图bm =新位图(pictureBox1.Width,pictureBox1.Height);


在绘图过程中绘制到Bitmap而不是PictureBox

最后,将整个Bitmap绘制到PictureBox(ps,PictureBox是

用于显示图像,而不是用于绘图。不重要,但它有点像用麻炮射击麻雀。我更喜欢一个简单的面板,我是
可以四处移动。您可以直接在任何控件上绘图一个

Windows窗体)


图形g = Graphics.FromImage(bm);

g.DrawLine(myPen,new Point(xStart,yStart),new Point(xEnd,yStart));
Create a Bitmap the size of your drawing board or any size you prefer.

Bitmap bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);

Inside your drawing procedure draw to the Bitmap instead of the PictureBox
In the end, draw the entire Bitmap to the PictureBox (ps, PictureBox is
meant for showing images, not to draw upon. Not that it matters, but it''s
a bit like shooting sparrows with cannons. I prefer a simple Panel that I
can move around. You can draw on any Control, including directly onto a
Windows Form)

Graphics g = Graphics.FromImage(bm);
g.DrawLine(myPen,new Point(xStart,yStart),new Point(xEnd,yStart));


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

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