所以基本上我想要做的是重疊兩個.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();
}
}
附上你到目前爲止嘗試過的一些代碼片段 – Daenarys
我已經把旋轉算法和透明度的東西,我還沒有實現它的任何代碼(面板事情我從形式編輯)。 – user3506622