Drag and Drop location zoomed

Questions, comments and suggestions concerning VintaSoft Annotation .NET Plug-in.

Moderator: Alex

Post Reply
derevell
Posts: 2
Joined: Mon Mar 24, 2014 8:25 pm

Drag and Drop location zoomed

Post by derevell » Mon Mar 24, 2014 8:29 pm

I can't seem to figure out the combination on how to drag and drop a note to the viewer in the correct location. I've tried the following but the annotation is always quite a bit off. Does anyone know how to translate the X and Y coordinates for the drop event to the location of the annotation?

Thanks,
Dave

Code: Select all

private void annotationViewer1_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.StringFormat))
    {
        var str = (string)e.Data.GetData(DataFormats.StringFormat);

        if (str.Length > 0)
        {

            float zoomFactor = ((annotationViewer1.ZoomExtended -100)/100);

            Point clientLocation = annotationViewer1.PointToClient(new Point(e.X, e.Y));

            PointF imageLocation = annotationViewer1.PointToImageF(new PointF(e.X, e.Y));

            PointF test1 = annotationViewer1.PointToImageF(new PointF(clientLocation.X, clientLocation.Y));

            float clientx = clientLocation.X * zoomFactor;
            float clienty = clientLocation.Y;

            StickyNoteAnnotationData stickyNote = new StickyNoteAnnotationData();
            stickyNote.FillBrush = new AnnotationSolidBrush(Color.Yellow);
            stickyNote.CollapsedTextData.Text = str;
            stickyNote.CollapsedTextData.TextPadding = new PaddingF(0f);
            stickyNote.CollapsedTextData.Font = new AnnotationFont("Tahoma", 12);
            stickyNote.CollapsedTextData.AutoSize = true;
            stickyNote.CanMirror = false;
            stickyNote.CanResize = false;
            stickyNote.CollapsedTextData.Font.Underline = true;
            stickyNote.Outline.Color = Color.Red;
            stickyNote.CollapsedTextData.TextAlign = ContentAlignment.MiddleCenter;


            stickyNote.Location = test1;

            annotationViewer1.AddAndBuildAnnotation(stickyNote);
        }
    }
}

derevell
Posts: 2
Joined: Mon Mar 24, 2014 8:25 pm

Re: Drag and Drop location zoomed

Post by derevell » Tue Mar 25, 2014 12:58 am

For those that run in to this; here is the solution

Code: Select all

// Get the cordinates of the point on screen to client
Point clientLocation = annotationViewer1.PointToClient(new Point(e.X, e.Y));

// Get the point of the image in the viewer
PointF imageLocation = annotationViewer1.PointToImageF(new PointF(clientLocation.X, clientLocation.Y));

// Convert the resolution to the image
float multX = annotationViewer1.Image.Resolution.Horizontal/96;
float multY = annotationViewer1.Image.Resolution.Vertical/96;
float x = imageLocation.X/multX;
float y = imageLocation.Y/multY;

var stickyNote = new StickyNoteAnnotationData();
stickyNote.FillBrush = new AnnotationSolidBrush(Color.Yellow);
stickyNote.CollapsedTextData.Text = str;
stickyNote.CollapsedTextData.TextPadding = new PaddingF(0f);
stickyNote.CollapsedTextData.Font = new AnnotationFont(defaultFont.Name, defaultFont.Size);
stickyNote.CollapsedTextData.AutoSize = true;
stickyNote.CanMirror = false;
stickyNote.CanResize = false;
stickyNote.CollapsedTextData.Font.Underline = true;
stickyNote.Outline.Color = Color.Red;
stickyNote.CollapsedTextData.TextAlign = ContentAlignment.MiddleCenter;

// Adjust for text width
Size textSize = TextRenderer.MeasureText(str, defaultFont);
// DPI Adjust
float w = (float)Math.Ceiling((float)textSize.Width / 96f * 100f);

stickyNote.Location = new PointF(x + (w / 2) - 20, y);

annotationViewer1.AddAndBuildAnnotation(stickyNote);

Alex
Site Admin
Posts: 1462
Joined: Thu Jul 10, 2008 2:21 pm

Re: Drag and Drop location zoomed

Post by Alex » Tue Mar 25, 2014 8:41 am

Hello Dave,

Your code is correct. First, you converted screen coordinates to the viewer coordinates. Next, you converted viewer coordinates to the image coordinates. Finally, you converted image coordinates to the annotation coordinates in DIP.

Best regards, Alexander

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest