두 개의 라이브러리를 함께 사용하고 있습니다. 하나는 System.Windows.Media.Imaging.BitmapSource
유형의 출력을 제공하고 다른 하나는 System.Drawing.Image
유형의 입력 만 허용합니다.System.Windows.Media.Imaging.BitmapSource를 System.Drawing.Image로 변환하십시오.
이 변환은 어떻게 수행 될 수 있습니까?
두 개의 라이브러리를 함께 사용하고 있습니다. 하나는 System.Windows.Media.Imaging.BitmapSource
유형의 출력을 제공하고 다른 하나는 System.Drawing.Image
유형의 입력 만 허용합니다.System.Windows.Media.Imaging.BitmapSource를 System.Drawing.Image로 변환하십시오.
이 변환은 어떻게 수행 될 수 있습니까?
private System.Drawing.Bitmap BitmapFromSource(BitmapSource bitmapsource)
{
System.Drawing.Bitmap bitmap;
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(outStream);
bitmap = new System.Drawing.Bitmap(outStream);
}
return bitmap;
}
이것은 동일한 작업을 수행하는 대체 기술입니다. 대답은 받아 들여지지 만 알파 채널이있는 이미지에는 문제가 발생합니다 (PngBitmapEncoder로 전환 한 후에도 문제가 발생 함). 이 기술은 호환되는 픽셀 형식으로 변환 한 후에 픽셀의 원본 복사본을 수행하기 때문에 더 빠를 수도 있습니다.
public Bitmap BitmapFromSource(System.Windows.Media.Imaging.BitmapSource bitmapsource)
{
//convert image format
var src = new System.Windows.Media.Imaging.FormatConvertedBitmap();
src.BeginInit();
src.Source = bitmapsource;
src.DestinationFormat = System.Windows.Media.PixelFormats.Bgra32;
src.EndInit();
//copy to bitmap
Bitmap bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
src.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bitmap.UnlockBits(data);
return bitmap;
}
투명성이 손실됩니다 (알파 채널이있는 비트 맵의 경우). – sibvic