[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

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: [Tutorial]Creating Images from Rectangle Selection Via Mouse

Post by Gabriela » Mon Apr 01, 2019 8:18 am

Here you can find another useful example of usage of highlighted regions through the GdViewer.
Drawing highlighted regions within the GdViewer using the mouse

LeonDuerr
Posts: 1
Joined: Mon Nov 18, 2019 6:56 am

Re: [Tutorial]Creating Images from Rectangle Selection Via Mouse

Post by LeonDuerr » Mon Nov 18, 2019 7:06 am

Hi there in the community, (because it's my first post)

thx a lot to SamiKharma and Gabriela, unfortunately the discribed way for getting the coordinates works only, if you load one page from a pdf-file by rendering the picture. But what, if I load the whole pdf-File by LoadFromFile into the viewer. There is no chance to detect the coordinates, not by GetRectCoordinatesOnDocument() neither by GetRectCoordinatesOnViewer().

Does anyone have an idea - how to do it??? I did the workaround by always loading the next page as an image on page-navigation. But I'm not really glad about it.

Best wishes to everyone,

Leon

Hugo
Posts: 227
Joined: Tue Dec 18, 2018 10:09 am

Re: [Tutorial]Creating Images from Rectangle Selection Via Mouse

Post by Hugo » Mon Nov 18, 2019 2:20 pm

Hi Leon,

Thanks for your question!

You can do this by using this method: https://www.gdpicture.com/guides/gdpicture/GdP ... nches.html

Depending on what you want to achieve do not hesitate on browsing all the GdViewer methods as you may find what you need.

Let me know if this solves your issue.

Regards

VPKLTony
Posts: 5
Joined: Tue May 05, 2020 5:08 am

Re: [Tutorial]Creating Images from Rectangle Selection Via Mouse

Post by VPKLTony » Tue May 05, 2020 5:12 am

Hi There,

I followed this thread to get the selection area from an image but eventually the coordinates returned are 0, can someone help ?

Inside the thread it did mention the 'rectangle', is it different with the rectangle that I knew ?

Thanks in advance.

Hugo
Posts: 227
Joined: Tue Dec 18, 2018 10:09 am

Re: [Tutorial]Creating Images from Rectangle Selection Via Mouse

Post by Hugo » Tue May 05, 2020 2:45 pm

Hi there,

What "rectangle" are you using? MouseModeAreaSelection is what you will be needing.

Regards,
Hugo Cudd

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest