如何在C#中读取.img文件



我正在开发一个Windows表单应用程序,其中我需要做的一件事就是从.img文件中提取图像。我能够读取普通的JPG和PNG文件,但不能读取.img文件。

我在Internet上找不到太多信息。我确实在MSDN上找到了一些代码,并试图使其工作。以下是正在抛出的代码和例外。

 FileInfo file = new FileInfo(FilePath.Text);
 FileStream f1 = new FileStream(FilePath.Text, FileMode.Open, 
 FileAccess.Read, FileShare.Read);
 byte[] BytesOfPic = new byte[Convert.ToInt32(file.Length)];
 f1.Read(BytesOfPic, 0, Convert.ToInt32(file.Length));
 MemoryStream mStream = new MemoryStream();
 mStream.Write(BytesOfPic, 0, Convert.ToInt32(BytesOfPic.Length));
 Bitmap bm = new Bitmap(mStream, false);
 mStream.Dispose();
 // ImageBox is name of a PictureBox
 ImageBox.image = bm;   // this line is throwing the error

例外

system.argumentException:参数无效。 在system.drawing.bitmap..ctor上 在c: users tiwar desktop a02-stegnography a02-stegnography a02-stegnography a02-stegnography form1.cs中,请访问c: users tiwar desktop tiwar desktop form1.cs:第65

很抱歉这是一个愚蠢的问题。我希望我已经提供了足够的信息,但是如果我不请告诉我。

FileInfo file = new FileInfo(FilePath.Text);
FileStream f1 = new FileStream(FilePath.Text, FileMode.Open, 
FileAccess.Read, FileShare.Read);
byte[] BytesOfPic = new byte[Convert.ToInt32(file.Length)];
f1.Read(BytesOfPic, 0, Convert.ToInt32(file.Length));
using (MemoryStream mStream = new MemoryStream())
{
    mStream.Write(BytesOfPic, 0, BytesOfPic.Length);
    mStream.Seek(0, SeekOrigin.Begin);
    Bitmap bm = new Bitmap(mStream);
    // ImageBox is name of a PictureBox
    ImageBox.image = bm;
}

您可以尝试我的解决方案

最新更新