简体   繁体   English

复制System.Drawing.Image类,无需参考

[英]Copy System.Drawing.Image class without reference

I need to make copy of Image class. 我需要复制Image类。 Tried clone() , but didn't work. 尝试了clone() ,但是没有用。 Also googled about it and found MemberwiseClone() , but i think i can't access Image class. 还谷歌搜索它,发现MemberwiseClone() ,但我认为我无法访问Image类。 Also tried this solution, guess it didn't work. 还尝试了解决方案,猜想它没有用。 Any ideas how to copy Image class without reference? 有什么想法如何复制Image类而不参考?

Bitmap (继承自Image具有构造函数重载,该重载会创建一个副本:

Bitmap copy = new Bitmap(image);

My bad. 我的错。 There was silly mistake. 有一个愚蠢的错误。 Solution below also works fine. 下面的解决方案也可以。

private Image DeepCopy(Image other)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ms, other);
                ms.Position = 0;
                return (Image)formatter.Deserialize(ms);
            }
        }

.

Image tempImg = DeepCopy(img);

Also, this works too: 此外,这也适用:

Image tempImg = (Image)img.Clone();

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

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