코드를 단순화 : DrawingImage
으로
DrawingImage drawingElement = (DrawingImage)System.Windows.Application.Current.TryFindResource(name);
BitmapSource btmSrc = drawingElement as BitmapSource;
이 BitmapSource는 상속하지 않습니다, 결과는 null가됩니다.
나는 (이렇게하지 않는 복사 - 붙여 넣기 솔루션으로 의사로 이것을 가지고) 테스트하기 위해 DrawingImage
이없는 있지만, 변환 코드는 다음과 같이 보일 것입니다 :
// Create a visual from a drawing
DrawingVisual drawingVisual = new DrawingVisual();
drawingVisual.Drawing.Children.Add(drawingImage.Drawing);
// Render it to a WPF bitmap
var renderTargetBitmap = new RenderTargetBitmap(
drawingVisual.Drawing.Bounds.Right,
drawingVisual.Drawing.Bounds.Bottom, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(drawingVisual);
// Create a bitmap with the correct size
Bitmap bmp = new Bitmap(renderTargetBitmap.PixelWidth,
renderTargetBitmap.PixelHeight, PixelFormat.Format32bppPArgb);
BitmapData data = bmp.LockBits(new Rectangle(Point.Empty, bmp.Size),
ImageLockMode.WriteOnly, PixelFormat.Format32bppPArgb);
renderTargetBitmap.CopyPixels(Int32Rect.Empty, data.Scan0,
data.Height * data.Stride, data.Stride);
bmp.UnlockBits(data);
마지막을 일부는 다음에서 가져온 것입니다. Is there a good way to convert between BitmapSource and Bitmap?
이보다 훨씬 많은 코드가 필요합니다. "이미지"란 무엇이며 어디에 정의되어 있습니까? –