2009-11-21 61 views

回答

4

不要试图窃取答案,请使用Johannes引用的CodeProject文章中给出的代码创建GDI位图。然后,您可以使用下面的代码把它转换成一个的BitmapSource用于WPF:

public static BitmapSource ToBitmapSource(this System.Drawing.Bitmap source) 
    { 
     var hBitmap = source.GetHbitmap(); 

     try 
     { 
      return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
       hBitmap, 
       IntPtr.Zero, 
       Int32Rect.Empty, 
       BitmapSizeOptions.FromEmptyOptions()); 
     } 
     catch (Win32Exception) 
     { 
      return null; 
     } 
     finally 
     { 
      NativeMethods.DeleteObject(hBitmap); 
     } 
    } 

其中用于NativeMethods.DeleteObject()的代码是:

[DllImport("gdi32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    internal static extern bool DeleteObject(IntPtr hObject); 
+0

你走了,你偷了答案,而不是约翰尼斯要去生你的气......看看你做了什么? :))只是开玩笑,感谢代码;) – luvieere 2009-11-22 15:35:31

+0

哈哈,没有probs:D – 2009-11-24 09:17:49

4

这里有一个CodeProject article

而且由于屏幕捕获不是WPF特定的,因此该解决方案也不涉及WPF。

+0

一个是知道的,我m对WPF解决方案感兴趣,这会导致BitmapImage或其他东西。 – luvieere 2009-11-21 23:54:24

+0

http://blogs.msdn.com/rwlodarc/archive/2007/01/03/wpf-bitmapsource-and-gdi-bitmap-interop.aspx – Joey 2009-11-22 00:26:02