0

Issue: After rotating a picturebox image - the image not the pbx - a new image assigned to the pbx will not display or update. The rotated image remains displayed in the pbx. After several "days" of experimenting and searching SO and others, I cannot find a solution to this.

The simplified code below illustrates the issue. I am undoubtedly overlooking something very simple - the question is "what"?.

Running C# Winforms .Net 4.7.2 using Visual Studio in Win10 1909 v18363.778

    private void btnOpen_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog OpenFileDialog = new OpenFileDialog())
        {
            OpenFileDialog.Title = "Open Image...";
            OpenFileDialog.Filter = "Image File (*.bmp; *.gif; *.jpg; *.jpeg; *.png; *.wmf)|*.*";
            OpenFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            if (OpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                srcImg = Image.FromFile(OpenFileDialog.FileName);                                                    
                tmpImg = srcImg;
                pbx1.Image = tmpImg;
            }
        }
    }

    private void btnRotate_Click(object sender, EventArgs e)
    {
        tmpImg.RotateFlip(RotateFlipType.Rotate90FlipNone);
        pbx1.Image = tmpImg;
    }

    private void btnReset_Click(object sender, EventArgs e)
    {
        // following code will not load original source image to "pbx1.Image"
        // the rotated image remains displayed...

        pbx1.Image = null;
        pbx1.Image = srcImg;
        pbx1.Refresh();
    }
galileo
  • 59
  • 7
  • You need to understand about reference varaibles. Replace this `tmpImg = srcImg;` by this: `tmpImg = (Image)srcImg.Clone();` - The former assigns another reference to the same one image. The latter creates a new copy. – TaW May 11 '20 at 18:11
  • 1
    Arrrgh...painfully obvious. The reference "linkage" between the objects needs to be broken. The issue is resolved and the code functions correctly. Thank you! – galileo May 11 '20 at 18:38

0 Answers0