2014-03-26 2 views
1

GDI + 사용 이미지로 구성된 간단한 사각형을 만들려고합니다. 이 직사각형이 이동합니다. 내가 겪어 본 몇 가지 문제점이 있습니다. 우선, 이미지를 로컬에서 참조하는 방법 (항상 복사하도록 설정 됨), 사각형을 가운데에 배치하는 방법 및 사각형이 움직일 때 이미지를 고정하는 방법은 무엇입니까? 이미지가 바둑판처럼 보이는 타일에 wrapMode.clamp의 IT 기본값을 사용하여 로컬 이미지를 참조하는 방법을 다음TextureBrush를 사용하여 이미지를 페인팅하는 방법

+0

내가 제목을 편집 한 것입니다 가정합니다. "[제목에"태그 "가 포함되어 있어야합니까?] (http://meta.stackexchange.com/questions/19190/)"합의가 "아니오, 그렇지 않아야합니다"로 표시되어야합니다. –

+0

정지라고 말하면 이미지가 움직일 때 이미지가 정사각형의 가운데에 위치한다는 의미입니까? –

+0

@MikeAbyss 네, 이상적으로 정사각형이 돌아 다니고 이미지가 항상 정사각형의 가운데에 배치됩니다. – Centimane

답변

2

에 하나 개의 이미지에서 사각형의 움직임을 이동하지 않고

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); 

(그 항상 복사하도록 설정 됨)

이미지를 리소스 파일에 추가 한 다음 코드에서 해당 이미지를 참조 할 수 있습니다. (링크 http://msdn.microsoft.com/en-us/library/7k989cfy%28v=vs.90%29.aspx 참조)

이미지가 광장을 중심으로하려면 어떻게 고정 할 때 사각형의 움직임을 이미지 을 유지하는 방법

? 이 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 

편집에 TranslateTransform을 사용하여 달성 할 수

: 그 이미지 크기는 항상 < = 광장 크기

+0

당신은 좋은 선생님을 좋아합니다. – Centimane

관련 문제