【问题标题】:How to Use TextureBrush for painting an Image如何使用 TextureBrush 绘制图像
【发布时间】:2014-05-05 13:50:09
【问题描述】:

使用 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 它默认为平铺,看起来图像是平铺的,移动方块从一个图像移动到下一个图像

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 当您说静止时,您的意思是图像在移动时在正方形内居中?
  • @MikeAbyss 是的,理想情况下,正方形会四处移动,并且图像将始终在正方形内居中。

标签: c# image bitmap gdi+


【解决方案1】:

如何在本地引用图片(设置为始终复制)

您可以将图像添加到资源文件,然后在代码中从那里引用该图像。 (见链接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

编辑:我假设图像大小总是

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-26
    • 2014-09-25
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多