图像保存在流格式图片框 [英] save image from picturebox in stream format

查看:166
本文介绍了图像保存在流格式图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存在PictureBox加载图片流。当我保存为PNG它正常工作,但是当我想它保存在其他格式,我得到



<块引用格式>

在GDI +的异常


发生一般性错误

其我的代码:

 图片IMG = pictureBox1.Image; 
字节[] = inputImage的新的字节[Img.Width * Img.Height]。
System.IO.MemoryStream毫秒​​=新System.IO.MemoryStream();
ms.Read(inputImage的0,Img.Width * Img.Height);

如果(System.Drawing.Imaging.ImageFormat.Jpeg.Equals(Img.RawFormat))
{
pictureBox1.Image.Save(MS,System.Drawing.Imaging。 ImageFormat.Jpeg);
}
,否则如果(System.Drawing.Imaging.ImageFormat.Gif.Equals(Img.RawFormat))
{
ms.Seek(0,SeekOrigin.Begin);
pictureBox1.Image.Save(MS,System.Drawing.Imaging.ImageFormat.Gif);
}
,否则如果(System.Drawing.Imaging.ImageFormat.Png.Equals(Img.RawFormat))
{
pictureBox1.Image.Save(MS,System.Drawing中。 Imaging.ImageFormat.Png);
}
,否则如果(System.Drawing.Imaging.ImageFormat.Bmp.Equals(Img.RawFormat))
{
pictureBox1.Image.Save(MS,System.Drawing中。 Imaging.ImageFormat.Bmp);
}


解决方案

试试这个方法:

 公共流ImageToStream(图片形象,System.Drawing.Imaging.ImageFormat格式)
{
的MemoryStream毫秒​​=新的MemoryStream();
image.Save(MS,格式);
返回毫秒;
}

和使用它:

 使用(流流= ImageToStream(pictureBox1.Image,
System.Drawing.Imaging.ImageFormat.Gif))
{
...
}


i wanna save a picture that loaded in a picturebox to stream .when i save in png format it work properly but when i want save it in other formats i get

A Generic error occured in GDI + exception

its my code:

Image Img = pictureBox1.Image;
byte[] inputImage = new byte[Img.Width * Img.Height];
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms.Read(inputImage, 0, Img.Width * Img.Height);

if (System.Drawing.Imaging.ImageFormat.Jpeg.Equals(Img.RawFormat))
{
    pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else if (System.Drawing.Imaging.ImageFormat.Gif.Equals(Img.RawFormat))
{
    ms.Seek(0, SeekOrigin.Begin);
    pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
}
else if (System.Drawing.Imaging.ImageFormat.Png.Equals(Img.RawFormat))
{
    pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
}
else if (System.Drawing.Imaging.ImageFormat.Bmp.Equals(Img.RawFormat))
{
    pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
}

解决方案

Try this method:

public Stream ImageToStream(Image image, System.Drawing.Imaging.ImageFormat format)
{
    MemoryStream ms = new MemoryStream();
    image.Save(ms, format);
    return  ms;
}

and use it:

using(Stream stream = ImageToStream(pictureBox1.Image, 
                           System.Drawing.Imaging.ImageFormat.Gif))
{
    ...
}

这篇关于图像保存在流格式图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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