转换图像字节格式 [英] convert image byte format

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

问题描述

大家好,
我正在尝试将图像转换为字节格式以将其保存到数据库中.
我正在使用sqlserver2008.
我写了一些代码.当我尝试逐行执行代码时,光标仅检查一行.光标从此处返回.该行如下:

Hello guys,
I am trying to convert image to byte format to save it into the database.
I am using sqlserver 2008.
I wrote some code. When I try to execute the code line by line, the cursor is checking only one line. From there cursor comes back. the line is below:

byte[] myimage = new byte[FUImage.PostedFile.ContentLength];



有人可以帮我吗?
编译器仅检查这一行.



Can anybody please help me.
The compiler is checking only this line. From here it is going to the catch block.

推荐答案

我猜你正在遇到空引用异常.
当您要执行该行时,请检查
的值 FUImageFUImage.PostedFileFUImage.PostedFile.ContentLength

其中之一可能为null.
另外,如果您提供了异常详细信息,则将更容易提供帮助.

希望这会有所帮助,
弗雷德里克(Fredrik)
I'' guessing you''re running into a null reference exception.
When you''re about to execute that line, check the values of
FUImage, FUImage.PostedFile and FUImage.PostedFile.ContentLength

One of those is probably null.
Also, it would be easier to provide help if you provided the exception details.

Hope this helps,
Fredrik


外观:
private static byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert,
ImageFormat formatOfImage)
{
byte[] Ret;

try
{

using (MemoryStream ms = new MemoryStream())
{
imageToConvert.Save(ms,formatOfImage);
Ret = ms.ToArray();
}
}
catch (Exception) { throw;}

return Ret;
}



准备好将字节数组转换回去时
图片,可以包含以下代码
在您的方法中.



When you are ready to convert the byte array back
to an image, you can include the following code
in your method.

System.Drawing.Image newImage;


using (MemoryStream ms = new MemoryStream(myByteArray,0,myByteArray.Length))
{

ms.Write(myByteArray,0,myByteArray.Length);

newImage = Image.FromStream(ms,true);

// work with image here.

// You'll need to keep the MemoryStream open for
// as long as you want to work with your new image.

}


这篇关于转换图像字节格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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