2009-04-23 12 views
1

アルファチャンネルが50%不透明に設定されている画像(PNGファイル)があります。 TransparencyKeyが白に設定され、バックカラーが白に設定されたフォームに画像を描画しようとすると、画像が50%シースルーで描画されることが予想されます。しかし、最初はフォームのバックカラーとブレンドされているため、完全に不透明です。これを回避する方法はありますか?私は、フォーム上のいくつかのイメージが半透明である必要があり、不透明である必要があるため、フォームの不透明プロパティを設定したくありません。画像付き透明Winform

答えて

0

できないと思います。私たちはこのようなことをしたスプラッシュスクリーンを持っていましたが、スクリーンをキャプチャしてフォームの背景イメージとして設定しました。明らかにこれはうまくいくように見えますが、画面が変わった場合、フォームの背景は変わらず、物事は変わって見えます。あなたがそれをする良い方法を見つけるなら、私はそれについて知りたいです。ここで

)は(単なるフォームのスクリーン座標と呼処理にScreenRectを設定し、画面をキャプチャするためのコードです:

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 

    } 
} 
0

ニース。 Vistaには半透明のウィンドウ(別名Areo)を作成するデスクトップウィンドウマネージャがあることを忘れないでください。http://msdn.microsoft.com/en-us/magazine/cc163435.aspx

+0

これは間違いありませんが、Windows XPと下位互換性のあるものが必要でした。 –

関連する問題