views:

1164

answers:

2

I'm working on a Map Editor for an XNA game I'm designing in my free time. The pieces of art used in the map are stored on a single texture and rectangles are stored with coordinates and widths etc.

In the winforms application I can add segments by selecting the segment I want from a listbox, which is populated from the array of possible segments.

Problem is I would like to be able to show a preview of the segment selected and, since it is stored on a common texture, I cant simply set a picturebox to display the image.

Is there anyway of using the rectangle information (.x, .y, .width, .height) to display only the section of the image in a picturebox, or to blit the section to a bitmap and display that?

Many Thanks

Michael Allen

A: 

You can use Graphics.DrawImage() and that will accept a Rectangle.

Nate Bross
+3  A: 

You probably want to look into the GDI library. Using the Image or Bitmap object and the Graphics.DrawImage() together will get what you're looking for.

private void DrawImageRectRect(PaintEventArgs e)
{

    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");

    // Create rectangle for displaying image.
    Rectangle destRect = new Rectangle(100, 100, 450, 150);

    // Create rectangle for source image.
    Rectangle srcRect = new Rectangle(50, 50, 150, 150);
    GraphicsUnit units = GraphicsUnit.Pixel;

    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destRect, srcRect, units);
}

You also might be interested in using XNA within your WinForm instead of using PictureBoxes and GDI. It's not 100% supported yet, but a tutorial on that can be found here.

Balk
Sorry, I should have made myself more clear. The Winforms application contains a XNA rendering component which displays the map using a system similar to the example you gave, from a class MapData. I use screen to Map coordinates for all map interaction and its working very well so far. Within the MapData map object is an array of Texture2, which contains all the textures the XNA component needs to draw the tile and segment sections. What i'm looking to do is take a rectangle from an array of segment definitions and render the part of a texture this defines within in the winforms application
as a preview of the segment or tile you've selected. I will try this approach but I'm not certain DrawImage can handle a Texture2
I'm not an XNA expert, I only breifly played around with it. That said, I would be using a Texture2D instead of whatever a "Texture2" is. Using the SpriteBatch, you can load your texture and use the Draw() function to achieve what you want. Good tutorial is found here: http://msdn.microsoft.com/en-us/library/bb194908.aspxMake sure to use the Draw() function that takes the destination and source rectangles.
Balk