打开并显示高清照片的WinForms应用程序 [英] Open and display a HD Photo in WinForms application

查看:229
本文介绍了打开并显示高清照片的WinForms应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个小程序,我想处理一些不同的图像类型 - 除其他事项外:高清图又名JPEG XR

I'm writing a small program where I would like to handle a number of different image types - among other things: "HD Photo" aka "JPEG XR".

我已经尝试了简单的 Image.FromFile(),但我得到一个 OutOfMemoryException异常。我试图寻找一些解决方案,但我已经找到了precious几个结果给我的怀疑,这可能仅适用于WPF应用程序。这是真的?如果没有,那么我怎么能打开这样的文件,这样我就可以把它放在一个图片框

I've tried a simple Image.FromFile() but I get an OutOfMemoryException. I've tried to search for some solutions but the precious few results I've found gave me the suspicion that this might only work in a WPF application. Is this true? If not then how can I open such a file so I can put it in a Picturebox?

推荐答案

我找到了一个可以接受的解决办法。我写了一个小WPF控件库加载高清照片,并返回一个System.Drawing.Bitmap。

I've found an acceptable workaround. I've written a small WPF Controls Library that loads HD Photos and returns a System.Drawing.Bitmap.

这是这和<一href="http://stackoverflow.com/questions/1337199/can-you-convert-a-system-windows-control-image-to-a-system-drawing-icon">this有一点扔在我自己的改进的问题。当我试图原始来源我有这样的图像消失了,当我调整了图片框的问题。它可能有一些做的只是指出一些阵列的图像信息。由图像绘制到第二个安全的位图,我设法摆脱这种效果。

It's a combination of this and this question with a bit of my own improvements thrown in. When I tried the original source I had the problem that the image vanished when I resized the picturebox. It probably has something to do with just pointing to some array for the image info. By drawing the image into a second safe Bitmap I managed to get rid of that effect.

public class HdPhotoLoader
{
    public static System.Drawing.Bitmap BitmapFromUri(String uri)
    {
        return BitmapFromUri(new Uri(uri, UriKind.Relative));
    }

    public static System.Drawing.Bitmap BitmapFromUri(Uri uri)
    {
        Image img = new Image();
        BitmapImage src = new BitmapImage();
        src.BeginInit();
        src.UriSource = uri;
        src.CacheOption = BitmapCacheOption.OnLoad;
        src.EndInit();
        img.Source = src;

        return BitmapSourceToBitmap(src);
    }

    public static System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource srs)
    {
        System.Drawing.Bitmap temp = null;
        System.Drawing.Bitmap result;
        System.Drawing.Graphics g;
        int width = srs.PixelWidth;
        int height = srs.PixelHeight;
        int stride = width * ((srs.Format.BitsPerPixel + 7) / 8);

        byte[] bits = new byte[height * stride];

        srs.CopyPixels(bits, stride, 0);

        unsafe
        {
            fixed (byte* pB = bits)
            {

                IntPtr ptr = new IntPtr(pB);

                temp = new System.Drawing.Bitmap(
                      width,
                      height,
                      stride,
                      System.Drawing.Imaging.PixelFormat.Format32bppPArgb,
                      ptr);
            }

        }

        // Copy the image back into a safe structure
        result = new System.Drawing.Bitmap(width, height);
        g = System.Drawing.Graphics.FromImage(result);

        g.DrawImage(temp, 0, 0);
        g.Dispose();

        return result;
    }
}

这篇关于打开并显示高清照片的WinForms应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆