C# · 12月 24, 2021

C#加载JPG文件,提取BitmapImage

我试图从JPG中提取Bitmap Image.这是我的代码: FileStream fIn = new FileStream(sourceFileName,FileMode.Open); // source JPGBitmap 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图像,这当然意味着它不起作用.我该怎么做?

解决方法 尝试这个: 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”);