[Tutorial]Creating Images from Rectangle Selection Via Mouse

Discussions about document viewing.
Post Reply
SamiKharma
Posts: 352
Joined: Tue Sep 27, 2011 11:47 am

[Tutorial]Creating Images from Rectangle Selection Via Mouse

Post by SamiKharma » Tue Jun 12, 2012 10:15 am

Hi All,

We have had many requests and questions about how to create a new image from a selection made by the user via the mouse.
Well, it is very simple, and we shall show you how here in this tutorial.

WHAT WE NEED:
1. A GdViewer called m_GdViewer
2. A GdPictureImaging Object called m_Imaging .
3. An Image loaded to the GdViewer, let us call it m_CurrentImage.
4. An Image to store the selected area in, let us call it m_SelectionImage

Ok, now first is first, we have to make sure that a user has made a selection to the Image on the GdViewer, for that we have a function:

Code: Select all

m_GdViewer.IsRect()
This function returns True if there is a selection and False if there is not.
Now, we need to create the parameters in which we will store the values of the location of the rectangle:

Code: Select all

int nLeft = 0, nTop = 0, nWidth = 0, nHeight = 0;
Those parameters, will be sent to the function which will tell us the position of the rectangle on the image, and not on the GdViewer. It is important to make the distinction, because the image changes size according to the zoom and the size of theGdViewer.
The function is:

Code: Select all

m_GdViewer.GetRectCoordinatesOnDocument(ref nLeft, ref nTop, ref nWidth, ref nHeight);
Now that we obtained those values, nothing is left but to create an image from the rectangle selected on the image, for that we have the following function:

Code: Select all

int m_SelectionImage= m_Imaging.CreateClonedGdPictureImageArea(m_CurrentImage, nLeft, nTop, nWidth, nHeight);
Now all you have to do is view the new image.

Code: Select all

m_GdViewer.DisplayFromGdPictureImage(m_SelectionImage);
Ok now let us put it all together:

Code: Select all

//We load the first image
m_CurrentImage = m_Imaging.CreateGdPictureImageFromFile("");
m_GdViewer.DisplayFromGdPictureImage(m_CurrentImage);

//You should have this code either on a Mouse_Up event, or in a button_Clicked event if the button is a crop button
if( m_GdViewer.IsRect())
{
   int nLeft = 0, nTop = 0, nWidth = 0, nHeight = 0;
   
   m_GdViewer.GetRectCoordinatesOnDocument(ref nLeft, ref nTop, ref nWidth, ref nHeight);
   int m_SelectionImage= m_Imaging.CreateClonedGdPictureImageArea(m_CurrentImage, nLeft, nTop, nWidth, nHeight);
   m_GdViewer.DisplayFromGdPictureImage(m_SelectionImage);
}
Hope you find this useful. If you have any questions, please do not hesitate to post them.
Best Regards,
Sami

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests