.net 在WinForms应用程序中打开并显示高清照片

.net 在WinForms应用程序中打开并显示高清照片,.net,winforms,image,.net,Winforms,Image,我正在写一个小程序,我想在其中处理许多不同的图像类型,其中包括:“HD Photo”又名“JPEG XR” 我尝试了一个简单的Image.FromFile(),但是我得到了一个OutOfMemoryException。我试图寻找一些解决方案,但我发现的宝贵的少数结果让我怀疑,这可能只适用于WPF应用程序。这是真的吗?如果没有,那么我如何打开这样的文件,以便将其放入图片盒?我找到了一个可接受的解决方法。我编写了一个小的WPF控件库,它加载高清照片并返回System.Drawing.Bitmap 这

我正在写一个小程序,我想在其中处理许多不同的图像类型,其中包括:“HD Photo”又名“JPEG XR”


我尝试了一个简单的
Image.FromFile()
,但是我得到了一个
OutOfMemoryException
。我试图寻找一些解决方案,但我发现的宝贵的少数结果让我怀疑,这可能只适用于WPF应用程序。这是真的吗?如果没有,那么我如何打开这样的文件,以便将其放入
图片盒

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

这是一个和问题的结合,加上我自己的一些改进。当我尝试原始来源时,我遇到了一个问题,当我调整picturebox的大小时,图像消失了。它可能与指向图像信息的某个数组有关。通过将图像绘制成第二个安全位图,我设法消除了这种效果

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

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

这是一个和问题的结合,加上我自己的一些改进。当我尝试原始来源时,我遇到了一个问题,当我调整picturebox的大小时,图像消失了。它可能与指向图像信息的某个数组有关。通过将图像绘制成第二个安全位图,我设法消除了这种效果

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应用程序中托管WPF控件的可能解决方案。我正在调查在我的Winforms应用程序中托管WPF控件的可能解决方案。太棒了!很高兴你找到了解决方案。太棒了!很高兴你找到了解决办法。