久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從 C# 中的位圖創(chuàng)建一個(gè)全新的位圖副本

Creating a completely new copy of bitmap from a bitmap in C#(從 C# 中的位圖創(chuàng)建一個(gè)全新的位圖副本)
本文介紹了從 C# 中的位圖創(chuàng)建一個(gè)全新的位圖副本的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我需要另一個(gè)位圖的位圖深層副本.現(xiàn)在,大多數(shù)解決方案都說(shuō)類(lèi)似this,這不是深拷貝.這意味著當(dāng)我鎖定原始位圖時(shí),副本也會(huì)被鎖定,因?yàn)榭寺∈窃嘉粓D的淺拷貝.現(xiàn)在以下似乎對(duì)我有用,但我不確定這是否適用于所有情況.

I need a deep copy of bitmap from another bitmap. Now, most of the solutions say something like this, which is not a deep copy. Meaning that when I lock the original bitmap, then the copy gets locked too, as the clone is a shallow copy of the original bitmap. Now the following seems to work for me, but I am not sure that will work in all cases.

public static Bitmap GetCopyOf(Bitmap originalImage)
{
    Rectangle rect = new Rectangle(0, 0, originalImage.Width, originalImage.Height);
    Bitmap retrunImage = new Bitmap(originalImage.Width, originalImage.Height, originalImage.PixelFormat);
    BitmapData srcData = originalImage.LockBits(rect, ImageLockMode.ReadOnly, originalImage.PixelFormat);
    BitmapData destData = retrunImage.LockBits(rect, ImageLockMode.WriteOnly, originalImage.PixelFormat);
    int dataLength = Math.Abs(srcData.Stride) * srcData.Height;
    byte[] data = new byte[dataLength];
    Marshal.Copy(srcData.Scan0, data, 0, data.Length);
    Marshal.Copy(data, 0, destData.Scan0, data.Length);
    destData.Stride = srcData.Stride;
    if (originalImage.Palette.Entries.Length != 0)
        retrunImage.Palette = originalImage.Palette;
    originalImage.UnlockBits(srcData);
    retrunImage.UnlockBits(destData);
    return retrunImage;
}

我需要更好、更優(yōu)雅的方式來(lái)做到這一點(diǎn).否則,請(qǐng)指出一些上述代碼可能會(huì)失敗的情況.TIA

I need better and more elegant way of doing this. Otherwise, just point me some cases where the above code may fail. TIA

推薦答案

我想我已經(jīng)通過(guò)使用這個(gè)片段解決了這個(gè)問(wèn)題.這個(gè)想法是由 Lanorkin 在評(píng)論中給出的,并且實(shí)現(xiàn)了 此處.希望這會(huì)在以后對(duì)某人有所幫助.

I think I have solved the problem by using this snippet. The idea was given by Lanorkin in the comment and the implementaion is found here. Hope this will help somebody later.

public static T Clone<T>(T source)
{
    if (!typeof(T).IsSerializable)
    {
        throw new ArgumentException("The type must be serializable.", "source");
    }

    // Don't serialize a null object, simply return the default for that object
    if (Object.ReferenceEquals(source, null))
    {
        return default(T);
    }

    IFormatter formatter = new BinaryFormatter();
    Stream stream = new MemoryStream();
    using (stream)
    {
        formatter.Serialize(stream, source);
        stream.Seek(0, SeekOrigin.Begin);
        return (T)formatter.Deserialize(stream);
    }
}

這篇關(guān)于從 C# 中的位圖創(chuàng)建一個(gè)全新的位圖副本的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應(yīng)用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動(dòng))
C# Console app - How do I make an interactive menu?(C# 控制臺(tái)應(yīng)用程序 - 如何制作交互式菜單?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創(chuàng)建重復(fù)的表單?)
UI Automation Control Desktop Application and Click on Menu Strip(UI自動(dòng)化控制桌面應(yīng)用程序并單擊菜單條)
Removing thin border around the menuitems(刪除菜單項(xiàng)周?chē)募?xì)邊框)
主站蜘蛛池模板: 性欧美xxxx| 黄色一级大片在线免费看产 | 99爱在线| 婷婷综合激情 | 欧美在线一区二区三区 | 中文字幕一区在线观看视频 | 久久99精品久久久久久国产越南 | 51ⅴ精品国产91久久久久久 | 一级在线毛片 | 亚洲一区二区免费看 | 成人性视频免费网站 | 激情五月婷婷丁香 | 成人福利在线视频 | 久草成人 | 亚洲福利 | 国产视频精品在线 | 日韩一区二区三区在线观看 | 天天曰夜夜操 | 亚洲h色 | 91久久精品一区二区三区 | 国产视频二区在线观看 | 欧美日韩一区二区三区不卡视频 | 中文字幕在线第一页 | 国产激情精品视频 | 国产精品国产三级国产播12软件 | 国产一区二区三区四区 | 日韩在线精品 | 手机在线观看 | 亚洲精品国产区 | 国产精品午夜电影 | 一区二区三区四区不卡 | 欧美成人一区二区三区片免费 | 国产999精品久久久久久 | 国产亚洲高清视频 | 一区二区三区国产精品 | www.亚洲一区| 精品久久国产 | 另类专区亚洲 | 久草在线青青草 | 久久久久国产精品一区二区 | 超碰导航 |