2016-07-25 9 views
-1

Ich mache gerade einen Screenshot einer externen Anwendung mit PrintWindow und ich möchte es auf meinem Desktop speichern.Bitmap.Speichern Sie die ganze Magier ist schwarz, nicht nur den Hintergrund

Dies ist mein Code:

// Get proc 
      Process proc = Process.GetProcessesByName("procName").Single(); 
      IntPtr hWnd = proc.MainWindowHandle; 

      // Restore proc if minimised 
      int style = GetWindowLong(hWnd, GWL_STYLE); 
      if ((style & WS_MINIMIZE) == WS_MINIMIZE) 
       ShowWindow(hWnd, WindowShowStyle.Restore); 

      // Get RECT 
      RECT rect; 
      GetWindowRect(new HandleRef(this, hWnd), out rect); 

      // Get screenshot 
      int width = rect.Right - rect.Left; 
      int height = rect.Bottom - rect.Top; 
      Bitmap bmp = new Bitmap(width, height); 
      using (Graphics g = Graphics.FromImage(bmp)) 
      { 
       IntPtr dc = g.GetHdc(); 

       if (!PrintWindow(hWnd, dc, 0)) 
       { 
        int error = Marshal.GetLastWin32Error(); 
        var exception = new System.ComponentModel.Win32Exception(error); 
        Debug.WriteLine("ERROR: " + error + ": " + exception.Message); 
        return; 
       } 

       //Thread.Sleep(200); 
       bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.jpeg", ImageFormat.Jpeg); 
       panel1.BackgroundImage = bmp; 
       g.ReleaseHdc(dc); 
      } 

Die panel1 zeigt ihm das gute Bild, die eigentliche Screenshot der Anwendung. Wenn ich auf meinen Desktop gehe, finde ich einen test.jpeg, aber es ist alles schwarz. Warum?

Danke!

+0

für ihn nur der Hintergrund war schwarz. Für mich ist das ganze Bild schwarz – Haytam

+0

"_ .. ** JPEG ** Bild hat standardmäßig einen schwarzen Hintergrund, wenn also Ihre Textfarbe auch schwarz ist, erhalten Sie ein schwarzes Bild. Wenn Ihr Bild keine Hintergrundfarbe hat, werden Sie muss es als ** PNG ** .._ speichern "Von [** Gespeichertes Bitmap ist schwarz **] (http://stackoverflow.com/questions/28019010/saved-bitmap-is-black) –

+0

Mein Bild ist ein Screenshot einer Anwendung, es enthält schwarzen Text, aber es ist viel mehr als nur schwarzer Text. – Haytam

Antwort

0

Ich habe bei dem Speichern als .jpg successed durch Änderung des Codes zu werden:

// Get screenshot 
      int width = rect.Right - rect.Left; 
      int height = rect.Bottom - rect.Top; 
      Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb); 
      using (Graphics g = Graphics.FromImage(bmp)) 
      { 
       g.Clear(Color.White); 
       IntPtr dc = g.GetHdc(); 

       if (!PrintWindow(hWnd, dc, 0)) 
       { 
        int error = Marshal.GetLastWin32Error(); 
        var exception = new System.ComponentModel.Win32Exception(error); 
        Debug.WriteLine("ERROR: " + error + ": " + exception.Message); 
        return; 
       } 

       g.ReleaseHdc(dc); 
      } 

      // Save the screenshot 
      bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.jpg", ImageFormat.Jpeg); 
      panel1.BackgroundImage = bmp; 
+0

Vergessen Sie nicht, das alte Bitmap-Objekt zu entfernen, das 'panel1.BackgroundImage' zugewiesen hat. Normalerweise würde ich etwas wie 'var temp = panel1.BackgroundImage; panel1.BackgroundImage = bmp; if (temp! = null) temp.Dispose(); ' –

Verwandte Themen