47

BitmapSourceからBitmapに変換する唯一の方法は、安全でないコードを使用することです...このように(Lesters WPFブログから):

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はビットマップで動作します。

4

6 に答える 6

71

ストレートからストレートにBitmap.LockBitsピクセルを使用してコピーすることにより、安全でないコードを使用せずに行うことが可能ですBitmapSourceBitmap

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;
}
于 2010-05-24T13:35:32.410 に答える
40

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

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

于 2014-02-21T10:31:39.130 に答える
1

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

Bitmap bmp = System.Drawing.Image.FromHbitmap(pBits);
于 2010-02-17T22:23:34.477 に答える
1

ここでは、リソースディクショナリ内の任意のビットマップリソースに透明な背景を設定するコードを示します(Windows.Forms時代によく使用されるResources.resxではありません)。InitializeComponent()-methodeの前にこのmethodeを呼び出します。メソッド'ConvertBitmap(Bitmap source)'およびBitmapFromSource(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;
                }
            }

        }

    }
于 2014-11-18T11:35:37.340 に答える
1

これはきちんとしていて、光よりも速いです:

  return Imaging.CreateBitmapSourceFromHBitmap( bitmap.GetHbitmap(), IntPtr.Zero,
      Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions() );
于 2015-06-10T12:12:04.527 に答える
0

両方の名前空間間でpixeldataを共有できます。変換する必要はありません。

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

于 2016-06-23T13:12:07.990 に答える