C#加载JPG文件,提取BitmapImage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10330239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:25:41 来源:igfitidea点击:
C# Load JPG file, extract BitmapImage
提问by IamIC
I am trying to extract a BitmapImage from a JPG. This is the code I have:
我正在尝试从 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();
image comes back with a 0 × 0 image, which of course means it didn't work. How do I do this?
image 返回一个 0 × 0 的图像,这当然意味着它不起作用。我该怎么做呢?
采纳答案by Eugene
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");

