2014-03-26 51 views
1

使用GDI +我试图制作一个由图像组成的简单正方形。这个矩形将被移动。我遇到了一些问题。首先,如何在图像中局部引用图像(设置为始终复制),如何让图像以正方形为中心,以及如何在方块移动时保持图像静止?如何使用TextureBrush绘制图像

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

我编辑了自己的冠军。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –

+0

当你说静止的时候,你的意思是图像在它移动的方块中居中。 –

+0

@MikeAbyss是的,理想情况下,广场会四处移动,图像将始终居中在广场内。 – Centimane

回答

2

如何在本地参考图像(它设置为始终复制)

您可以将图像添加到资源文件,然后从该代码内引用该图像。 (见链接http://msdn.microsoft.com/en-us/library/7k989cfy%28v=vs.90%29.aspx

如何获得的图像在广场中心,以及如何保持图像 固定方移动时?

这可以通过使用TranslateTransform与displayArea位置 (见链接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 

编辑来实现:我认为影像尺寸始终是< =方头尺寸

+0

你很好,先生 – Centimane