Scaled Adornments Demo question

Started by Wojciewa, March 16, 2017, 06:56:08 PM

Previous topic - Next topic

Wojciewa

Hi,
what should be done to have unscaled adornments, but they will hold the position over the image during resizing or moving it ?

Regards
Wojtek

Richard Moss

Hello,

Welcome to the forums, and thanks for the question. I updated the demonstration source on GitHub with the solution, but I'll print the appropriate code below.

Basically, you need to make use of the GetOffsetPoint method to determine the point that your adornment is attached to. Then, you offset that position based on the size of the adornment - you don't need to scale the size if you don't want to.

Here's example code taken from the updated demo which will always place the adorned above and centered to the adornment point. If the scaleAdornmentsCheckBox control is checked, the adornment will be scaled to fit the current zoom level, otherwise it will be painted at its default size.

    private void imageBox_Paint(object sender, PaintEventArgs e)
    {
      Graphics g;
      GraphicsState originalState;
      Size scaledSize;
      Size originalSize;
      Size drawSize;
      bool scaleAdornmentSize;

      scaleAdornmentSize = scaleAdornmentsCheckBox.Checked;

      g = e.Graphics;

      originalState = g.Save();

      // Work out the size of the marker graphic according to the current zoom level
      originalSize = _markerImage.Size;
      scaledSize = imageBox.GetScaledSize(originalSize);
      drawSize = scaleAdornmentSize ? scaledSize : originalSize;

      foreach (Point landmark in _landmarks)
      {
        Point location;

        // Work out the location of the marker graphic according to the current zoom level and scroll offset
        location = imageBox.GetOffsetPoint(landmark);

        // adjust the location so that the image is displayed above the location and centered to it
        location.Y -= drawSize.Height;
        location.X -= drawSize.Width >> 1;

        // Draw the marker
        g.InterpolationMode = InterpolationMode.NearestNeighbor;
        g.DrawImage(_markerImage, new Rectangle(location, drawSize), new Rectangle(Point.Empty, originalSize), GraphicsUnit.Pixel);
      }

      g.Restore(originalState);
    }


Hope this helps.

Regards;
Richard Moss
Read "Before You Post" before posting (https://forums.cyotek.com/cyotek-webcopy/before-you-post/). Do not send me private messages. Do not expect instant replies.

All responses are hand crafted. No AI involved. Possibly no I either.