C# Load JPG file, extract BitmapImage

为君一笑 提交于 2020-01-11 04:26:07

问题


I am trying to extract a BitmapImage from a JPG. This is the code I have:

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();

image comes back with a 0 × 0 image, which of course means it didn't work. How do I do this?


回答1:


Try this:

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
    }
}

Or you can just do this (source):

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


来源:https://stackoverflow.com/questions/10330239/c-sharp-load-jpg-file-extract-bitmapimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!