如何使用Visual Studio c#移动图片框的特定部分
本文关键字:定部 Visual 何使用 Studio 移动 | 更新日期: 2023-09-27 18:14:28
我想只移动我的图片框的某一部分,但我只能移动某一部分。目前我可以从左到右移动它,但我只想移动它的尖端。
private float angle = 0.0f;
Image image;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right) angle += 1;
else if (e.KeyCode == Keys.Left) angle -= 1;
int a = pictureBox1.Location.X;
int b = pictureBox1.Location.Y;
if (e.KeyCode == Keys.Right) a += 5;
else if (e.KeyCode == Keys.Left) a -= 5;
pictureBox2.Location = new Point(a, b);
RotateImage(pictureBox1, image, angle);
}
private void RotateImage(PictureBox pb, Image img, float angle)
{
//Store our old image so we can delete it
Image oldImage = pb.Image;
pb.Image = RotateImage(img, angle);
if (oldImage != null)
oldImage.Dispose();
}
public static Bitmap RotateImage(Image image, float angle)
{
return RotateImageFinal(image, new PointF((float)image.Width / 2, (float)image.Height / 2), angle);
}
public static Bitmap RotateImageFinal(Image image, PointF offset, float angle)
{
//create a new empty bitmap to hold rotated image
Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(rotatedBmp);
//Put the rotation point in the center of the image
g.TranslateTransform(offset.X, offset.Y);
//rotate the image
g.RotateTransform(angle);
//move the image back
g.TranslateTransform(-offset.X, -offset.Y);
//draw passed in image onto graphics object
g.DrawImage(image, new PointF(0, 0));
return rotatedBmp;
}
private void Form1_Load(object sender, EventArgs e)
{ //Just to store the address of the 'arrow' image.
string path = Directory.GetCurrentDirectory();
image = new Bitmap(path + "/img/arrow.bmp");
}
我想要倾斜的对象的PS,我感兴趣的是固定底部的部分,同时只移动顶部https://i.stack.imgur.com/aiJCF.jpg
我对你的问题很感兴趣,所以我决定花一些时间在c#中搜索更多关于图像旋转的信息。这就是结果。
private float angle = 0.0f;
Image image;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right) angle+=1;
else if (e.KeyCode == Keys.Left) angle-=1;
int a = ballPB.Location.X;
int b = ballPB.Location.Y;
if (e.KeyCode == Keys.Right) a += 5;
else if (e.KeyCode == Keys.Left) a -= 5;
ballPB.Location = new Point(a, b);
RotateImage(arrowPB, image, angle);
}
private void RotateImage(PictureBox pb, Image img, float angle)
{
//Store our old image so we can delete it
Image oldImage = pb.Image;
pb.Image = RotateImage(img, angle);
if (oldImage != null)
oldImage.Dispose();
}
public static Bitmap RotateImage(Image image, float angle)
{
return RotateImageFinal(image, new PointF((float)image.Width / 2, (float)image.Height / 2), angle);
}
public static Bitmap RotateImageFinal(Image image, PointF offset, float angle)
{
//create a new empty bitmap to hold rotated image
Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(rotatedBmp);
//Put the rotation point in the center of the image
g.TranslateTransform(offset.X, offset.Y);
//rotate the image
g.RotateTransform(angle);
//move the image back
g.TranslateTransform(-offset.X, -offset.Y);
//draw passed in image onto graphics object
g.DrawImage(image, new PointF(0, 0));
return rotatedBmp;
}
private void Form1_Load(object sender, EventArgs e)
{ //Just to store the address of the 'arrow' image.
string path = Directory.GetCurrentDirectory();
image = new Bitmap(path + "''img''arrow.png");
}
逻辑的功劳归于MusicMonkey5555 (http://www.codeproject.com/Articles/58815/C-Image-PictureBox-Rotations)。如果上面的代码不是很清楚,您甚至可以从链接下载源代码。好运!