简体   繁体   中英

Strange Image variable behavior

I have two PictureBoxes where I want to show an image in the first and then a rotated version in the second. The code I would believe should work does act strange however, as the image is rotated in both PictureBoxes :

Image im = Image.FromFile(D:\somefolder\picture.jpg");            
pictureBox1.Image = im;
Image im_rot = im;
//Image im_rot = Image.FromFile(D:\somefolder\picture.jpg");
im_rot.RotateFlip(RotateFlipType.Rotate270FlipNone);
pictureBox2.Image = im_rot;

If I replace line 3 with line 4 it works, but why doesn't it work the other way?

The way your code is currently written, you're assigning the same object to both variables. This means that when you operate on either one, it's changing the same object in memory. With the alternate code you have commented out, you create a new, distinct object which is assigned to each variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM