Page 1 of 1

Drag and Drop location zoomed

Posted: Mon Mar 24, 2014 8:29 pm
by derevell
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);
        }
    }
}

Re: Drag and Drop location zoomed

Posted: Tue Mar 25, 2014 12:58 am
by derevell
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);

Re: Drag and Drop location zoomed

Posted: Tue Mar 25, 2014 8:41 am
by Alex
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