web-dev-qa-db-ja.com

BitmapSourceとBitmapの間で変換する良い方法はありますか?

私が知る限り、BitmapSourceからBitmapに変換する唯一の方法は、安全でないコードを使用することです...このように(from Lesters WPF blog ):

myBitmapSource.CopyPixels(bits, stride, 0);

unsafe
{
  fixed (byte* pBits = bits)
  {
      IntPtr ptr = new IntPtr(pBits);

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

      return bitmap;
  }
}

逆にするには:

System.Windows.Media.Imaging.BitmapSource bitmapSource =
  System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
    bitmap.GetHbitmap(),
    IntPtr.Zero,
    Int32Rect.Empty,
    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

フレームワークに簡単な方法はありますか?それがそこにない理由は何ですか(ない場合)?かなり使いやすいと思います。

必要な理由は、AForgeを使用してWPFアプリで特定の画像操作を行うためです。 WPFはBitmapSource/ImageSourceを表示したいのですが、AForgeはビットマップで動作します。

40
JohannesH

Bitmap.LockBitsを使用して安全でないコードを使用せずに実行し、BitmapSourceからBitmapにピクセルを直接コピーすることができます。

Bitmap GetBitmap(BitmapSource source) {
  Bitmap bmp = new Bitmap(
    source.PixelWidth,
    source.PixelHeight,
    PixelFormat.Format32bppPArgb);
  BitmapData data = bmp.LockBits(
    new Rectangle(Point.Empty, bmp.Size),
    ImageLockMode.WriteOnly,
    PixelFormat.Format32bppPArgb);
  source.CopyPixels(
    Int32Rect.Empty,
    data.Scan0,
    data.Height * data.Stride,
    data.Stride);
  bmp.UnlockBits(data);
  return bmp;
}
63
josef.axa

次の2つの方法を使用できます。

public static BitmapSource ConvertBitmap(Bitmap source)
{
    return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                  source.GetHbitmap(),
                  IntPtr.Zero,
                  Int32Rect.Empty,
                  BitmapSizeOptions.FromEmptyOptions());
}

public static Bitmap BitmapFromSource(BitmapSource bitmapsource)
{
    Bitmap bitmap;
    using (var outStream = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapsource));
        enc.Save(outStream);
        bitmap = new Bitmap(outStream);
    }
    return bitmap;
}

それは私にとって完璧に機能します。

30
melvas

これはあなたが探しているものですか?

Bitmap bmp = System.Drawing.Image.FromHbitmap(pBits);
1
caesay

これは光よりもきれいで高速です:

  return Imaging.CreateBitmapSourceFromHBitmap( bitmap.GetHbitmap(), IntPtr.Zero,
      Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions() );
1
Mauro Sampietro

ここでは、リソースディクショナリ内のビットマップリソースに透明な背景を設定するコード(Windows.Forms時代によく使用されるResources.resxではありません)。 InitializeComponent()-methodeの前にこのメソッドを呼び出します。メソッド 'ConvertBitmap(Bitmap source)'およびBitmapFromSource(BitmapSource bitmapsource)は、上記のmelvasの投稿で言及されています。

private void SetBitmapResourcesTransparent()
    {
        Image img;
        BitmapSource bmpSource;
        System.Drawing.Bitmap bmp;
        foreach (ResourceDictionary resdict in Application.Current.Resources.MergedDictionaries)
        {
            foreach (DictionaryEntry dictEntry in resdict)
            {
                // search for bitmap resource
                if ((img = dictEntry.Value as Image) is Image 
                    && (bmpSource = img.Source as BitmapSource) is BitmapSource
                    && (bmp = BitmapFromSource(bmpSource)) != null)
                {
                    // make bitmap transparent and assign it back to ressource
                    bmp.MakeTransparent(System.Drawing.Color.Magenta);
                    bmpSource = ConvertBitmap(bmp);
                    img.Source = bmpSource;
                }
            }

        }

    }
1
user3318309

両方のネームスペース間でピクセルデータを共有できます。変換する必要はありません。

SharedBitmapSourceを使用します。 https://stackoverflow.com/a/32841840/690656

0
Andreas