2009-04-23 10 views
1

J'ai une image (fichier PNG) dont le canal alpha est réglé sur 50% opaque. Lorsque j'essaie de dessiner l'image sur un formulaire qui a la TransparencyKey définie sur blanc et la couleur de fond définie sur blanc, j'attends que l'image soit dessinée à 50% de transparence. Cependant, il est d'abord mélangé avec les formes backcolor et par conséquent il est complètement opaque. Y at-il un moyen de contourner cela? Je ne veux pas définir la propriété Opaque du formulaire car certaines images du formulaire doivent être translucides et certaines doivent être opaques.Winform transparent avec l'image

Répondre

0

Je ne pense pas que vous le pouvez. Nous avons un écran de démarrage où nous avons fait quelque chose comme ça, mais nous avons fini par capturer l'écran et le définir comme image de fond du formulaire. Évidemment, cela ne semble fonctionner que si l'écran change, l'arrière-plan du formulaire ne fonctionne pas et les choses semblent bizarres. Si vous trouvez une meilleure façon de le faire, j'aimerais le savoir.

Voici le code pour capturer l'écran, vient de mettre la ScreenRect aux formes coordonnées d'écran et processus d'appel():

using System; 
using System.Drawing; 
using System.Runtime.InteropServices; 

namespace TourFactory.Core.Drawing 
{ 
    public class CaptureScreenCommand 
    { 

     #region Initialization and Destruction 

     public CaptureScreenCommand() 
     { 
     } 

     #endregion 

     #region Fields and Properties 

     // BitBlt is a multipurpose function that takes a ROP (Raster OPeration) code 
     // that controls exactly what it does. 0xCC0020 is the ROP code SRCCOPY, i.e. 
     // do a simple copy from the source to the destination. 
     private const int cRasterOp_SrcCopy = 0xCC0020; // 13369376; 

     private Rectangle mScreenRect; 
     /// <summary> 
     /// Gets or sets the screen coordinates to capture. 
     /// </summary> 
     public Rectangle ScreenRect 
     { 
      get { return mScreenRect; } 
      set { mScreenRect = value; } 
     } 

     #endregion 

     #region Methods 

     public Image Process() 
     { 
      // use the GDI call and create a DC to the whole display 
      var dc1 = CreateDC("DISPLAY", null, 0, 0); 
      var g1 = Graphics.FromHdc(dc1); 

      // create a compatible bitmap the size of the form 
      var bmp = new Bitmap(mScreenRect.Width, mScreenRect.Height, g1); 
      var g2 = Graphics.FromImage(bmp); 

      // Now go retrace our steps and get the device contexts for both the bitmap and the screen 
      // Note: Apparently you have to do this, and can't go directly from the aquired dc or exceptions are thrown 
      // when you try to release the dcs 
      dc1 = g1.GetHdc(); 
      var dc2 = g2.GetHdc(); 

      // Bit Blast the screen into the Bitmap 
      BitBlt(dc2, 0, 0, mScreenRect.Width, mScreenRect.Height, dc1, mScreenRect.Left, mScreenRect.Top, cRasterOp_SrcCopy); 

      // Remember to release the dc's, otherwise problems down the road 
      g1.ReleaseHdc(dc1); 
      g2.ReleaseHdc(dc2); 

      // return bitmap 
      return bmp; 
     } 

     #endregion 

     #region gdi32.dll 

     [DllImport("gdi32")] 
     private static extern IntPtr CreateDC(string lpDriverName, string lpDeviceName, int lpOutput, int lpInitData); 

     [DllImport("gdi32")] 
     private static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int width, int height, IntPtr hdcSrc, int xSrc, int ySrc, int dwRop); 

     #endregion 

    } 
}