[Tutorial]Creating Images from Rectangle Selection Via Mouse
Posted: 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:
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:
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:
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:
Now all you have to do is view the new image.
Ok now let us put it all together:
Hope you find this useful. If you have any questions, please do not hesitate to post them.
Best Regards,
Sami
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()
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;
The function is:
Code: Select all
m_GdViewer.GetRectCoordinatesOnDocument(ref nLeft, ref nTop, ref nWidth, ref nHeight);
Code: Select all
int m_SelectionImage= m_Imaging.CreateClonedGdPictureImageArea(m_CurrentImage, nLeft, nTop, nWidth, nHeight);
Code: Select all
m_GdViewer.DisplayFromGdPictureImage(m_SelectionImage);
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);
}
Best Regards,
Sami