2010-06-28 38 views
3

我想要caputure一個控件的圖像,這是一個ActiveX導入控件,並在WPF中顯示該圖像。 我不使用Windows的原因形成主機是實在是太慢了(我想創建這些ActiveX控件列表):到目前爲止,我有:如何將從Windows窗體控件創建的位圖添加到BitmapSource?

public Bitmap CaptureCtrl(Control ctrl) 
    { 
     // Ein neues Image mit der grösse des jeweiligen Controls anlegen 
     Console.WriteLine("new Bitmap({0:d}, {0:d}, g)", ctrl.Size.Width, ctrl.Size.Height); 
     Bitmap newImage = new Bitmap(ctrl.Size.Width, ctrl.Size.Height); 

     Graphics memoryGraphics = Graphics.FromImage(newImage); 
     //Handle holen 
     var hwnd = ctrl.Handle; 
     IntPtr src = NativeMethods.GetDC(hwnd); 
     IntPtr dest = memoryGraphics.GetHdc(); 

     // das Handle des Ziels 
     // die X Position an der das Bild eingefügt werden soll 
     // die Y Position an der das Bild eingefügt werden soll 
     // die breite des Bildes 
     // die höhe des Bildes 
     // das Handle der Quelle 
     // die X Position an der das Control liegt 
     // die Y Position an der das Control liegt 
     // Raster Operation Code an dieser Stelle ist es SRCCOPY 
     // Natürlich muß der auf der Seite angegebene Hex wert noch in ein int umgerechnet werden 
     // mehr informationen auf http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceui40/html/_celrfternaryrasteroperations.asp 
     Console.WriteLine("BitBlt(dest, 0, 0, {0:d}, {1:d}, src, {2:d}, {3:d}, 13369376)", ctrl.ClientRectangle.Width, ctrl.ClientRectangle.Height, ctrl.Location.X, ctrl.Location.Y); 
     NativeMethods.BitBlt(dest, 0, 0, ctrl.ClientRectangle.Width, ctrl.ClientRectangle.Height, src, ctrl.Location.X, ctrl.Location.Y, 13369376); 

     // Handles wieder Freigeben 
     NativeMethods.ReleaseDC(hwnd, src); 
     memoryGraphics.ReleaseHdc(dest); 

     return newImage; 
    } 

和:

/// <summary> 
    /// Converts a <see cref="System.Drawing.Bitmap"/> into a WPF <see cref="BitmapSource"/>. 
    /// </summary> 
    /// <remarks>Uses GDI to do the conversion. Hence the call to the marshalled DeleteObject. 
    /// </remarks> 
    /// <param name="source">The source bitmap.</param> 
    /// <returns>A BitmapSource</returns> 
    public static System.Windows.Media.Imaging.BitmapSource ToBitmapSource(this System.Drawing.Bitmap source) 
    { 
     System.Windows.Media.Imaging.BitmapSource bitSrc = null; 

     IntPtr hBitmap = source.GetHbitmap(); 

     try 
     { 
      bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
       hBitmap, 
       IntPtr.Zero, 
       Int32Rect.Empty, 
       System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); 
     } 
     catch (Win32Exception we) 
     { 
      bitSrc = null; 
      Console.WriteLine(we.ToString()+ " data: "+we.Data + " code: " + we.ErrorCode); 
     } 
     finally 
     { 
      NativeMethods.DeleteObject(hBitmap); 
     } 

     return bitSrc; 
    } 

和調用代碼:

using (var capture = host.CaptureCtrl(this.control)) 
    { 
     this.image.Source = capture.ToBitmapSource(); 
    } 

的NativeMethod調用從DLL的進口,應該很清楚。 現在的問題是,圖片只是灰色,並沒有充滿內容(似乎確實有東西複製)

回答

0

我找到了一個解決方案(不一定是當前版本中的錯誤) ,有興趣的人士......

如果caputure方法被替換爲這一點,那麼它就像一個魅力:

public Bitmap CaptureCtrl(Control ctrl) 
{ 
    // Ein neues Image mit der grösse des jeweiligen Controls anlegen 
    Bitmap newImage = new Bitmap(ctrl.Size.Width, ctrl.Size.Height); 
    ctrl.DrawToBitmap(newImage, ctrl.ClientRectangle); 
    return newImage; 
} 
+0

我要指出,我畫的組件在Windows窗體框架,該框架必須是可見= true(否則我得到一個OCX狀態異常)。 – Sebastian 2010-06-28 19:04:57

+0

我不明白。上面新的CaptureCtrl方法是從控件創建位圖的標準方法。這是如何工作的ActiveX控件? – adamcodes 2011-03-17 16:39:00

+0

是不是一個ActiveX控件也是一個控件? – Sebastian 2011-03-18 07:02:15

相關問題