简体   繁体   English

奇怪的图像变量行为

[英]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. 我有两个PictureBoxes ,我想在第一个中显示图像,然后在第二个中显示旋转版本。 The code I would believe should work does act strange however, as the image is rotated in both PictureBoxes : 我认为应该工作的代码确实表现得很奇怪,因为在两个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? 如果我将第3行替换为第4行,它会起作用,但是为什么不行呢?

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. 使用已注释掉的备用代码,您可以创建一个新的,不同的对象,并将其分配给每个变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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