0

I have 2 pictureBoxes on my Form. In the right pictureBox I can zoom and pan an image. The illustration on the left shows only the image. Unfortunately I can‘t copy the zoomed image from pictureBox1(right) into pictureBox2. How should i do that? I’m thankful for any hint!

public Form1()
{
  InitializeComponent();

  string imgFile = @"C:\Users\Pictures\Clock.jpg";
  img = Image.FromFile(imgFile);

  Graphics g = this.CreateGraphics();

  zoom = ((float)pictureBox1.Width / (float)img.Width) * (img.HorizontalResolution / g.DpiX);

  pictureBox1.Paint += new PaintEventHandler(ImageBox_Paint);

}

private void ImageBox_Paint(object sender, PaintEventArgs e)
{
   e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
   e.Graphics.ScaleTransform(zoom, zoom);              
   e.Graphics.DrawImage(img, imgX, imgY);                      
}

private void Button1_Click(object sender, EventArgs e)
{          
  Bitmap newbmp= new Bitmap(pictureBox2.ClientSize.Width,pictureBox2.ClientSize.Height);

  using (Bitmap oldbmp = new Bitmap(img, pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height))
  {

    using (Graphics g = Graphics.FromImage(newbmp))
    {
       g.DrawImage(oldbmp, new Rectangle(0, 0, 
       pictureBox2.ClientSize.Width,pictureBox2.ClientSize.Height));                
    }

  }          
  pictureBox2.Image = newbmp;

}   

enter image description here

Mike19
  • 95
  • 6
  • You have defined a scale factor: use this multiplier to redefine the Image size instead of using the dimensions of Controls used to present it. – Jimi Apr 22 '21 at 22:39
  • Jimi, can you explain this in a little bit of code to me? It is not possible to use the dimensions of Controls instead of using a scale factor? Thank you. – Mike19 Apr 23 '21 at 08:13

0 Answers0