2015-11-04 38 views
5

透明な背景から試してみると、完全に透明ではありません。 この問題で2つのコードブロックを試しました。まず私は、このコードのように試してみました:C#Windowsフォームの透明な背景イメージ

public Form1() 
    { 
     InitializeComponent(); 
     SetStyle(ControlStyles.SupportsTransparentBackColor, true); 
     this.BackColor = Color.Transparent; 
     this.FormBorderStyle = FormBorderStyle.None; 
     //this.WindowState = System.Windows.Forms.FormWindowState.Maximized; 

    } 

それがこの絵のように見えます。

enter image description here

、私はいくつかの異なるコードを発見し、IKEこの試み。

public Form1() 
    { 
     InitializeComponent(); 
     this.TransparencyKey = Color.White; 
     this.BackColor = Color.White; 
     this.FormBorderStyle = FormBorderStyle.None; 
     this.WindowState = System.Windows.Forms.FormWindowState.Maximized; 

    } 

これはこの写真のようです。

enter image description here

あなたは白のボーダーとロゴを見ることができます。私は.pngロゴだけを完全に透明にしたい。私は何をすべきか?どのようにこれを行うことができますか?

私のロゴイメージは.pngです。

enter image description here

+0

その絵は何を変える介して画像の透明度のレベルを選択することができますか?それは背景イメージですか? –

+0

画像ボックスの背景画像です。実際には、プログラムの開始前に歓迎メッセージを出したいと思っています... – CoderWho

+0

その画像を共有できますか? –

答えて

7

あなたはレイヤードウィンドウを使用してLayered Windows

を使用することができ非常に複雑な形状を持つウィンドウのパフォーマンスと視覚効果を向上させることができ、その形状をアニメーション化し、または影響をアルファブレンディングを使用したいです。システムは、階層化されたウィンドウと基礎となるアプリケーションのウィンドウを自動的に合成して再ペイントします。その結果、複雑なウィンドウ領域の典型的なちらつきなしに、レイヤードウィンドウが滑らかにレンダリングされます。さらに、レイヤードウィンドウは部分的に半透明、つまりアルファブレンドされています。

Windowsでレイヤードウィンドウを作成します。ここ

フォームWindowsフォームにレイヤードのWindowsを作成する実証msdn code galleryからいくつかのコードです。これは、あなたが形のスプラッシュ画面を作成し、マウスでそれを移動させることができます。

PerPixelAlphaFormをプロジェクトに追加してから、このフォームから継承し、SelectBitmapを呼び出して、pngをメソッドに渡してレイヤードウィンドウを作成してください。

enter image description here

PerPixelAlphaForm.cs

#region Using directives 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing.Imaging; 
using System.Runtime.InteropServices; 
#endregion 
namespace CSWinFormLayeredWindow 
{ 
    public partial class PerPixelAlphaForm : Form 
    { 
     public PerPixelAlphaForm() 
     { 
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 
      this.ShowInTaskbar = false; 
      this.StartPosition = FormStartPosition.CenterScreen; 
      this.Load += PerPixelAlphaForm_Load; 
     } 

     void PerPixelAlphaForm_Load(object sender, EventArgs e) 
     { 
      this.TopMost = true; 
     } 
     protected override CreateParams CreateParams 
     { 
      get 
      { 
       // Add the layered extended style (WS_EX_LAYERED) to this window. 
       CreateParams createParams = base.CreateParams; 
       if(!DesignMode) 
        createParams.ExStyle |= WS_EX_LAYERED; 
       return createParams; 
      } 
     } 
     /// <summary> 
     /// Let Windows drag this window for us (thinks its hitting the title 
     /// bar of the window) 
     /// </summary> 
     /// <param name="message"></param> 
     protected override void WndProc(ref Message message) 
     { 
      if (message.Msg == WM_NCHITTEST) 
      { 
       // Tell Windows that the user is on the title bar (caption) 
       message.Result = (IntPtr)HTCAPTION; 
      } 
      else 
      { 
       base.WndProc(ref message); 
      } 
     } 
     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="bitmap"></param> 
     public void SelectBitmap(Bitmap bitmap) 
     { 
      SelectBitmap(bitmap, 255); 
     } 
     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="bitmap"> 
     /// 
     /// </param> 
     /// <param name="opacity"> 
     /// Specifies an alpha transparency value to be used on the entire source 
     /// bitmap. The SourceConstantAlpha value is combined with any per-pixel 
     /// alpha values in the source bitmap. The value ranges from 0 to 255. If 
     /// you set SourceConstantAlpha to 0, it is assumed that your image is 
     /// transparent. When you only want to use per-pixel alpha values, set 
     /// the SourceConstantAlpha value to 255 (opaque). 
     /// </param> 
     public void SelectBitmap(Bitmap bitmap, int opacity) 
     { 
      // Does this bitmap contain an alpha channel? 
      if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) 
      { 
       throw new ApplicationException("The bitmap must be 32bpp with alpha-channel."); 
      } 

      // Get device contexts 
      IntPtr screenDc = GetDC(IntPtr.Zero); 
      IntPtr memDc = CreateCompatibleDC(screenDc); 
      IntPtr hBitmap = IntPtr.Zero; 
      IntPtr hOldBitmap = IntPtr.Zero; 

      try 
      { 
       // Get handle to the new bitmap and select it into the current 
       // device context. 
       hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); 
       hOldBitmap = SelectObject(memDc, hBitmap); 

       // Set parameters for layered window update. 
       Size newSize = new Size(bitmap.Width, bitmap.Height); 
       Point sourceLocation = new Point(0, 0); 
       Point newLocation = new Point(this.Left, this.Top); 
       BLENDFUNCTION blend = new BLENDFUNCTION(); 
       blend.BlendOp = AC_SRC_OVER; 
       blend.BlendFlags = 0; 
       blend.SourceConstantAlpha = (byte)opacity; 
       blend.AlphaFormat = AC_SRC_ALPHA; 

       // Update the window. 
       UpdateLayeredWindow(
        this.Handle,  // Handle to the layered window 
        screenDc,  // Handle to the screen DC 
        ref newLocation, // New screen position of the layered window 
        ref newSize,  // New size of the layered window 
        memDc,   // Handle to the layered window surface DC 
        ref sourceLocation, // Location of the layer in the DC 
        0,    // Color key of the layered window 
        ref blend,  // Transparency of the layered window 
        ULW_ALPHA  // Use blend as the blend function 
        ); 
      } 
      finally 
      { 
       // Release device context. 
       ReleaseDC(IntPtr.Zero, screenDc); 
       if (hBitmap != IntPtr.Zero) 
       { 
        SelectObject(memDc, hOldBitmap); 
        DeleteObject(hBitmap); 
       } 
       DeleteDC(memDc); 
      } 
     } 
     #region Native Methods and Structures 

     const Int32 WS_EX_LAYERED = 0x80000; 
     const Int32 HTCAPTION = 0x02; 
     const Int32 WM_NCHITTEST = 0x84; 
     const Int32 ULW_ALPHA = 0x02; 
     const byte AC_SRC_OVER = 0x00; 
     const byte AC_SRC_ALPHA = 0x01; 

     [StructLayout(LayoutKind.Sequential)] 
     struct Point 
     { 
      public Int32 x; 
      public Int32 y; 

      public Point(Int32 x, Int32 y) 
      { this.x = x; this.y = y; } 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     struct Size 
     { 
      public Int32 cx; 
      public Int32 cy; 

      public Size(Int32 cx, Int32 cy) 
      { this.cx = cx; this.cy = cy; } 
     } 

     [StructLayout(LayoutKind.Sequential, Pack = 1)] 
     struct ARGB 
     { 
      public byte Blue; 
      public byte Green; 
      public byte Red; 
      public byte Alpha; 
     } 

     [StructLayout(LayoutKind.Sequential, Pack = 1)] 
     struct BLENDFUNCTION 
     { 
      public byte BlendOp; 
      public byte BlendFlags; 
      public byte SourceConstantAlpha; 
      public byte AlphaFormat; 
     } 

     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, 
      ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, 
      Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags); 

     [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     static extern IntPtr CreateCompatibleDC(IntPtr hDC); 

     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     static extern IntPtr GetDC(IntPtr hWnd); 

     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); 

     [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     static extern bool DeleteDC(IntPtr hdc); 

     [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); 

     [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     static extern bool DeleteObject(IntPtr hObject); 

     #endregion 
    } 
} 

SplashScreen.cs

public partial class Form4 : CSWinFormLayeredWindow.PerPixelAlphaForm 
{ 
    public Form4() 
    { 
     InitializeComponent(); 
     this.SelectBitmap(Properties.Resources.splash); 
    } 
} 

元の答えはダブルバッファをオフにして、OnPaintBackgroundを無効にし、ベースメソッドを呼び出さずに画像を描画することに基づいていました。答えには既知の問題がありました。フォームはモアレスでしたが、うまくいきましたが、フォームが動いていたりフォームの後ろにあるウィンドウが変更された場合、ウィンドウは更新されませんでした。リビジョンの前のコードを見ることができます。 MSDNコードに完全に基づいている現在の編集には、既知の問題はありません。

+0

しかし問題があります:Sこれを試したとき。これは次のようになります:http://i66.tinypic.com/9knl9d.png – CoderWho

+0

イメージで見てきたように、 'SetStyle'の後に' InitializeComponent(); 'を実行しました。あなたは 'InitializeComponent();'の後に 'SetStyle'を書かなければなりません。 –

+0

私はあなたが言うようにコードを修正しました。しかし同じことをもう一度 – CoderWho

0

あなたもbackgrounfイメージをすることができ、0〜255の画像の間にあるアルファ係数、無PROB

private static Image ToGrayscale(Image s,int alpha) 
     { 
      Bitmap tImage = new Bitmap(s); 

      for (int x = 0; x < tImage.Width; x++) 
      { 
       for (int y = 0; y < tImage.Height; y++) 
       { 
        Color tCol = tImage.GetPixel(x, y); 
        Color newColor = Color.FromArgb(alpha, tCol.R, tCol.G, tCol.B); 
        tImage.SetPixel(x, y, newColor); 
       } 
      } 
      return tImage; 

     }