在C#中覆盖PictureBox的图像 [英] Overwrite image picturebox in C#

查看:1039
本文介绍了在C#中覆盖PictureBox的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序,
我添加一个图片来添加图片到一些产品,我有一个问题,我想编辑已经添加到一个产品的图片,我怎么能做到这一点?
这是我真正的代码。

 私人无效pbImagenEquipo_DoubleClick(对象发件人,EventArgs五)
{
ofdImagenes.Filter =Imagenes JPG(* .JPG)| * .JPG; * .JPEG; | Imagenes PNG格式(* .png)| *。PNG
的DialogResult RESP = ofdImagenes.ShowDialog();
如果(RESP == DialogResult.OK)
{
位图B =新位图(ofdImagenes.FileName);
的String [] = archivo ofdImagenes.FileName.Split('。');
NOMBRE =Equipo_+ lbID +。 + archivo [archivo.Length-1];

b.Save(Path.Combine(Application.StartupPath,Imagenes农布雷));

pbImagenEquipo.Image = B;

}
}



但是当我尝试更换像我得到这个错误:

 键入'System.Runtime.InteropServices.ExternalException的未处理的异常出现在System.Drawing中。 DLL 

附加信息:在电子GDI +错误generoc。


解决方案

这是一个常见的​​问题。



借助文档说:




图片保存到它从构建同一个文件是不是
允许并抛出例外。




有两种选择。一个是写它之前删除的文件。



另一种方法是使用流写。我更喜欢后者..:

 字符串FN =D:\\xyz.jpg 

//读取图像文件
图像oldImg = Image.FromFile(FN);

//做一些事情(可选;-)
((位图)oldImg).SetResolution(123,234);

//保存到一个MemoryStream
的MemoryStream毫秒​​=新的MemoryStream();
oldImg.Save(MS,ImageFormat.Jpeg);

//处理旧图像
oldImg.Dispose();

//保存到相同的文件名
图像newImage = Image.FromStream(MS)的新形象;
newImage.Save(FN);

请注意,拯救 JPEG 文件通常达到更好质量,如果你采取的编码选项控制。使用此此重载 ..



还要注意,由于我们需要的图像处置您需要确保它不会在<$任何地方使用,如C $ C> PictureBox.Image !如果是,处理之前将其设置为有:<!code> pictureBox1.Image = NULL;



有关的解决方案删除旧文件中看到这里


I'm doing one application, i add a picturebox to add image to some products, i have one question, i would like edit the images already added to one product, how can i do that? This is my actually code.

private void pbImagenEquipo_DoubleClick(object sender, EventArgs e)
{
    ofdImagenes.Filter = "Imagenes JPG (*.jpg)|*.jpg; *.jpeg;|Imagenes PNG (*.png)|*.png";
    DialogResult resp = ofdImagenes.ShowDialog();
    if (resp == DialogResult.OK)
    {
        Bitmap b = new Bitmap(ofdImagenes.FileName);
        string [] archivo = ofdImagenes.FileName.Split('.');
        nombre = "Equipo_" + lbID+ "." + archivo[archivo.Length-1];

        b.Save(Path.Combine(Application.StartupPath, "Imagenes", nombre));

        pbImagenEquipo.Image = b;

    }
}

But when i try to replace the image i got this error:

An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll

Additional information: Error generoc in e GDI+.

解决方案

This is a common issue.

The documentation says:

Saving the image to the same file it was constructed from is not allowed and throws an exception.

There are two options. One is to delete the file before writing it.

The other is to use a Stream to write it. I prefer the latter..:

string fn = "d:\\xyz.jpg";

// read image file
Image oldImg = Image.FromFile(fn);

// do something (optional ;-)
((Bitmap)oldImg).SetResolution(123, 234);

// save to a memorystream
MemoryStream ms = new MemoryStream();
oldImg.Save(ms, ImageFormat.Jpeg);

// dispose old image
oldImg.Dispose();

// save new image to same filename
Image newImage = Image.FromStream(ms);
newImage.Save(fn);

Note that saving jpeg files often achieves better quality if you take control of encoding options. Use this overload for this..

Also note that since we need to dispose of the image you need to make sure that it is not used anywhere, like in a PictureBox.Image! If it is, set it to null there before disposing : pictureBox1.Image = null; !

For a solution deleting the old file see here

这篇关于在C#中覆盖PictureBox的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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