문제

winforms system.windows.forms.control 클래스에는 인스턴스 방법 "drawtobitmap"이 있으며 다양한 상황에서 매우 유용하다고 생각합니다. WPF 응용 프로그램에서 System.Drawing.BitMap을 얻는 동등한 방법이 있는지 궁금합니다.

응용 프로그램 창을 얻기 위해 P/호출 물건을 수행 할 수 있다는 것을 알고 있지만 64 비트 전환을 잘 수용하지 않기 때문에이 점이 마음에 들지 않으며 DrawtobitMap과 같이 하위 제어 만 렌더링 할 수 없습니다. 하다.

고마워요, 리차드

도움이 되었습니까?

해결책

MSDN에서 RenderTargetBitMap을 사용하십시오

RenderTargetBitmap bitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(this.YourVisualControlNameGoesHere); 

다른 팁

TFD가 켜져 있습니다. MSDN에서 덜 우아한 참조 예제를 사용할 수도 있습니다.

Dim width As Integer = 128
Dim height As Integer = width
Dim stride As Integer = CType(width / 8, Integer)
Dim pixels(height * stride) As Byte

' Try creating a new image with a custom palette.
Dim colors As New List(Of System.Windows.Media.Color)()
colors.Add(System.Windows.Media.Colors.Red)
colors.Add(System.Windows.Media.Colors.Blue)
colors.Add(System.Windows.Media.Colors.Green)
Dim myPalette As New BitmapPalette(Colors)

' Creates a new empty image with the pre-defined palette
Dim image As BitmapSource = System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96, 96, PixelFormats.Indexed1, myPalette, pixels, stride)
Dim stream As New FileStream("new.bmp", FileMode.Create)
Dim encoder As New BmpBitmapEncoder()
Dim myTextBlock As New TextBlock()
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString()
encoder.Frames.Add(BitmapFrame.Create(image))
encoder.Save(stream)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top