2014-04-08 241 views
1

所以基本上我想要做的是重疊兩個.PNG圖像與透明背景。一個是用霰彈槍旋轉到鼠標位置,另一個是我想放在霰彈槍後面的卡通人物。現在,問題是我每次重疊它們時,PNG圖像的透明背景都會遮擋,我根本無法看到射手。覆蓋PNG圖像保持透明

我試圖把射手放在面板中,但放置在它內部的霰彈槍picturebox擰旋轉算法(使其旋轉非常緩慢),我不知道爲什麼。

任何幫助將是apreciated,謝謝。

編碼我用: 旋轉算法:

private Bitmap rotateImage(Bitmap b, float angle) 
{ 
    //create a new empty bitmap to hold rotated image 
    Bitmap returnBitmap = new Bitmap(b.Width, b.Height); 
    //make a graphics object from the empty bitmap 
    Graphics g = Graphics.FromImage(returnBitmap); 
    //move rotation point to center of image 
    g.TranslateTransform((float)b.Width/2, (float)b.Height/2); 
    //rotate 
    g.RotateTransform((int)angle); 
    //move image back 
    g.TranslateTransform(-(float)b.Width/2, -(float)b.Height/2); 
    //draw passed in image onto graphics object 
    g.DrawImage(b, new Point(0, 0)); //??? 
    return returnBitmap; 
} 

private float CalcAngle(Point TargetPos) 
{ 
    Point ZeroPoint = new Point(pictureBox1.Location.X + pictureBox1.Width/2, pictureBox1.Location.Y + pictureBox1.Height/2); 
    if (TargetPos == ZeroPoint) 
    { 
     return 0; 
    } 

    double angle; 
    double deltaX, deltaY; 

    deltaY = TargetPos.Y - ZeroPoint.Y; 
    deltaX = TargetPos.X - ZeroPoint.X; 

    angle = Math.Atan2(deltaY, deltaX) * 180/Math.PI; 
    return (float)angle; 
} 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    pictureBox1.Image = (Bitmap)backup.Clone(); 
    //Load an image in from a file 
    Image image = new Bitmap(pictureBox1.Image); 
    //Set our picture box to that image 
    pictureBox1.Image = (Bitmap)backup.Clone(); 

    //Store our old image so we can delete it 
    Image oldImage = pictureBox1.Image; 
    //Set angle 
    angle = CalcAngle(new Point(Cursor.Position.X, Cursor.Position.Y - 10)); 
    //Pass in our original image and return a new image rotated X degrees right 
    pictureBox1.Image = rotateImage((Bitmap)image, angle); 
    if (oldImage != null) 
    { 
     oldImage.Dispose(); 
     image.Dispose(); 
    } 
} 
+0

附上你到目前爲止嘗試過的一些代碼片段 – Daenarys

+0

我已經把旋轉算法和透明度的東西,我還沒有實現它的任何代碼(面板事情我從形式編輯)。 – user3506622

回答

0

當您創建一個新的位圖嘗試使用任何像素格式爲32 BPP或64 BPP。請參見下面的代碼:

Bitmap returnBitmap = new Bitmap(b.Width, b.Height, PixelFormat.Format64bppPArgb); 
0

這裏我借鑑彼此頂部的三個不同的PNG文件到面板:

using (Graphics graphic = panel1.CreateGraphics()) 
{ 
    using (Image image = Image.FromFile(@"D:\tp3.png")) graphic.DrawImage(image, Point.Empty); 
    using (Image image = Image.FromFile(@"D:\tp2.png")) graphic.DrawImage(image, Point.Empty); 
    using (Image image = Image.FromFile(@"D:\tp1.png")) graphic.DrawImage(image, Point.Empty); 
} 

如果您創建一個新的位圖作爲做@ Palak.Maheria說和使用一個帶有alpha通道的32位格式!

+0

謝謝:)必須對DrawImage函數進行更多的研究,並且我沒有使用面板,但是您的答案有很大幫助。問候 :) – user3506622