私は適切な回避策を見つけました。私は、HD Photosをロードし、System.Drawing.Bitmapを返す小さなWPF Controlsライブラリを作成しました。
thisとthisという質問の組み合わせがあります。私は元のソースを試してみましたが、画像のサイズを変更すると画像が消えてしまうという問題がありました。おそらく、画像情報の配列を指すだけのことがあります。イメージを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;
}
}
私はWinformsアプリケーションでWPFコントロールをホストする可能性のある回避策について検討しています。 – Kempeth