【问题标题】:C# Load JPG file, extract BitmapImageC#加载JPG文件,提取BitmapImage
【发布时间】:2012-05-06 23:57:07
【问题描述】:

我正在尝试从 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 图像,这当然意味着它不起作用。我该怎么做?

【问题讨论】:

    标签: c# image-processing bitmap jpeg bitmapimage


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 我发现我的错误:我需要使用 Image,而不是 BitmapImage。我知道它不需要复杂!这是工作代码: Image image = Image.FromFile(sourceFileName, true);
    • 男孩一开始就这么复杂! Image、BitmapImage、ImageSource、JpegImageDecoder 等。
    • 这是我们的工作保障,呵呵 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 2010-09-07
    • 2010-09-10
    相关资源
    最近更新 更多