使用GDI +我试图制作一个由图像组成的简单方块。此矩形将被移动。我遇到了一些问题。首先,如何局部参考图像(设置为始终复制),如何使图像居中于正方形,以及如何在正方形移动时保持图像静止?
Bitmap runnerImage = (Bitmap)Image.FromFile(@"newRunner.bmp", true);//this results in an error without full path
TextureBrush imageBrush = new TextureBrush(runnerImage);
imageBrush.WrapMode = System.Drawing.Drawing2D.WrapMode.Clamp;//causes the image to get smaller/larger if movement is tried
Graphics.FillRectangle(imageBrush, displayArea);
不使用wrapMode.clamp,它默认为平铺,看起来像图像是平铺的,移动方块从一个图像移动到下一个
答案 0 :(得分:2)
如何在本地引用图像(设置为始终复制)
您可以将图像添加到资源文件中,然后在代码中从那里引用该图像。 (参见链接http://msdn.microsoft.com/en-us/library/7k989cfy%28v=vs.90%29.aspx)
如何将图像置于正方形中心,以及如何保持图像 在广场移动时静止不动?
这可以使用带有displayArea位置的TranslateTransform来实现 (参见链接http://msdn.microsoft.com/en-us/library/13fy233f%28v=vs.110%29.aspx)
TextureBrush imageBrush = new TextureBrush(runnerImage);
imageBrush.WrapMode = WrapMode.Clamp;//causes the image to get smaller/larger if movement is tried
Rectangle displayArea = new Rectangle(25, 25, 100, 200); //Random values I assigned
Point xDisplayCenterRelative = new Point(displayArea.Width / 2, displayArea.Height / 2); //Find the relative center location of DisplayArea
Point xImageCenterRelative = new Point(runnerImage.Width / 2, runnerImage.Height / 2); //Find the relative center location of Image
Point xOffSetRelative = new Point(xDisplayCenterRelative.X - xImageCenterRelative.X, xDisplayCenterRelative.Y - xImageCenterRelative.Y); //Find the relative offset
Point xAbsolutePixel = xOffSetRelative + new Size(displayArea.Location); //Find the absolute location
imageBrush.TranslateTransform(xAbsolutePixel.X, xAbsolutePixel.Y);
e.Graphics.FillRectangle(imageBrush, displayArea);
e.Graphics.DrawRectangle(Pens.Black, displayArea); //I'm using PaintEventArgs graphics
编辑:我认为图像尺寸始终为< =方形尺寸