C#加载JPG文件,解压缩BitmapImage

时间:2012-04-26 08:59:27

标签: c# image-processing bitmap jpeg bitmapimage

我正在尝试从JPG中提取BitmapImage。这是我的代码:

FileStream fIn = new FileStream(sourceFileName, FileMode.Open); // source JPG
Bitmap dImg = new Bitmap(fIn);
MemoryStream ms = new MemoryStream();
dImg.Save(ms, ImageFormat.Jpeg);
image = new BitmapImage();
image.BeginInit();
image.StreamSource = new MemoryStream(ms.ToArray());
image.EndInit();
ms.Close();

图像以0×0图像返回,这当然意味着它不起作用。我该怎么做?

1 个答案:

答案 0 :(得分:22)

试试这个:

public void Load(string fileName) 
{

    using(Stream BitmapStream = System.IO.File.Open(fileName,System.IO.FileMode.Open ))
    {
         Image img = Image.FromStream(BitmapStream);

         mBitmap=new Bitmap(img);
         //...do whatever
    }
}

或者你可以这样做(source):

Bitmap myBmp = Bitmap.FromFile("path here");