我正在尝试使用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的情况下,默认情况下为平铺,这看起来像是平铺了图像,并将正方形从一个图像移动到下一图像
如何本地引用图像(设置为始终复制)
您可以将图像添加到资源文件,然后在代码中从那里引用该图像。(请参阅链接http://msdn.microsoft.com/en-us/library/7k989cfy%28v=vs.90%29.aspx)
如何使图像居中于正方形,以及如何在正方形移动时保持图像静止?
这可以通过使用带有displayArea位置的TranslateTransform来实现(请参阅链接http://msdn.microsoft.com/zh-cn/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
编辑:我认为图像大小始终为<=正方形大小
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句