使用MemoryStream将字节数组转换为图像的问题 [英] Problem of converting a byte array to image using MemoryStream
本文介绍了使用MemoryStream将字节数组转换为图像的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这段代码将图片加载到我的应用中并且它没有正确显示图像
i am loading images to my app using this piece of code and it doesn't show the image correctly
Image [] img = new Image[100];
int i=0 ;
while (reader.Read())
{
string filePath = (string)reader["Path"];
byte[] transactionContext = (byte[])reader["TransactionContext"];
SqlFileStream sqlFileStream = new SqlFileStream(filePath, transactionContext, FileAccess.Read);
byte[] data = new byte[(int)sqlFileStream.Length];
sqlFileStream.Read(data, 0, Convert.ToInt32(sqlFileStream.Length));
sqlFileStream.Close();
MemoryStream ms = new MemoryStream();
ms.Write(data, 0, data.Length);
img[i] = byteArrayToImage(ms);
ms.Seek(0, SeekOrigin.Begin);
ms.Dispose();
ms.Close();
ms = null;
data = null;
i++;
}
和byteArrayToImage方法:
and byteArrayToImage method :
public Image byteArrayToImage(MemoryStream ms)
{
Image returnImage = Image.FromStream(ms);
return returnImage;
}
但是当我使用此代码将它们保存在磁盘上时,它可以很好地工作:
but when i save them on the disk using this code it works perfectly :
Image [] img = new Image[100];
int i=0 ;
while (reader.Read())
{
string filePath = (string)reader["Path"];
byte[] transactionContext = (byte[])reader["TransactionContext"];
SqlFileStream sqlFileStream = new SqlFileStream(filePath, transactionContext, FileAccess.Read);
byte[] data = new byte[(int)sqlFileStream.Length];
sqlFileStream.Read(data, 0, Convert.ToInt32(sqlFileStream.Length));
sqlFileStream.Close();
string filename = @"D:\Temp\pic" + i.ToString()+".jpeg";
i++;
System.IO.FileStream fs = new System.IO.FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write);
fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
}
推荐答案
您忘了在写日期后将流回卷到位置0,所以你正在尝试读取当前流位置,其中没有数据。请查看代码示例:
http:// msdn .microsoft.com / zh-cn / library / system.io.memorystream.aspx [ ^ ]。
现在,注意行memStream.Seek(0,SeekOrigin.Begin);
你应该这样做。
但是,您的代码看起来多余。这样做:
You forgot to rewind the stream to position 0 after the date is written, so you are trying to read past the current stream position, where there is no data. Please look at the code sample:
http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx[^].
Now, pay attention of the linememStream.Seek(0, SeekOrigin.Begin);
you should do the same.
However, your code looks redundant. Do this:
System.Bitmap.Image myImage = new System.Bitmap.Image(sqlFileStream);
您的第二个样本(带文件)确认您的 sqlFileStream
包含正确的数据。
请参阅: http://msdn.microsoft.com/en- us / library / z7ha67kw.aspx [ ^ ]。
Your second sample (with file) confirms that your sqlFileStream
contains correct data.
Please see: http://msdn.microsoft.com/en-us/library/z7ha67kw.aspx[^].
关于文件编写的例子混淆了问题。
试试这个例子
C#图像到字节数组和字节数组到图像转换器类 [ ^ ]
Your example about file writing confuses the question.
Try this example
C# Image to Byte Array and Byte Array to Image Converter Class[^]
这篇关于使用MemoryStream将字节数组转换为图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文