Cyotek Forums

Source Code => Source Code => Topic started by: Wojciewa on March 16, 2017, 06:56:08 PM

Title: Scaled Adornments Demo question
Post by: Wojciewa on March 16, 2017, 06:56:08 PM
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
Title: Re: Scaled Adornments Demo question
Post by: Richard Moss on March 18, 2017, 03:45:15 PM
Hello,

Welcome to the forums, and thanks for the question. I updated the demonstration source on GitHub (https://github.com/cyotek/Cyotek.Windows.Forms.ImageBox/) 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