2011-04-12 11 views
1

私はいくつかの画像タイプ、特に「HD Photo」(別名「JPEG XR」)を処理したい小さなプログラムを作成しています。WinFormsアプリケーションでHD写真を開いて表示する

私は単純なImage.FromFile()を試しましたが、OutOfMemoryExceptionを取得しました。私はいくつかのソリューションを検索しようとしましたが、私が見つけた貴重な結果はWPFアプリケーションでしか機能しない可能性があるという疑念を与えました。これは本当ですか?そうでなければ、私はPictureboxに入れることができるように、どのようにそのようなファイルを開くことができますか?

+0

私はWinformsアプリケーションでWPFコントロールをホストする可能性のある回避策について検討しています。 – Kempeth

答えて

1

私は適切な回避策を見つけました。私は、HD Photosをロードし、System.Drawing.Bitmapを返す小さなWPF Controlsライブラリを作成しました。

thisthisという質問の組み合わせがあります。私は元のソースを試してみましたが、画像のサイズを変更すると画像が消えてしまうという問題がありました。おそらく、画像情報の配列を指すだけのことがあります。イメージを2番目の安全なビットマップに描画することによって、その効果を取り除くことができました。

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

恐ろしい!あなたの解決策を見つけたらうれしいです。 – FreeAsInBeer

関連する問題