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

從 C# 中的位圖創建一個全新的位圖副本

Creating a completely new copy of bitmap from a bitmap in C#(從 C# 中的位圖創建一個全新的位圖副本)
本文介紹了從 C# 中的位圖創建一個全新的位圖副本的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要另一個位圖的位圖深層副本.現在,大多數解決方案都說類似this,這不是深拷貝.這意味著當我鎖定原始位圖時,副本也會被鎖定,因為克隆是原始位圖的淺拷貝.現在以下似乎對我有用,但我不確定這是否適用于所有情況.

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;
}

我需要更好、更優雅的方式來做到這一點.否則,請指出一些上述代碼可能會失敗的情況.TIA

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

推薦答案

我想我已經通過使用這個片段解決了這個問題.這個想法是由 Lanorkin 在評論中給出的,并且實現了 此處.希望這會在以后對某人有所幫助.

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);
    }
}

這篇關于從 C# 中的位圖創建一個全新的位圖副本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應用程序 - 如何制作交互式菜單?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創建重復的表單?)
UI Automation Control Desktop Application and Click on Menu Strip(UI自動化控制桌面應用程序并單擊菜單條)
Removing thin border around the menuitems(刪除菜單項周圍的細邊框)
主站蜘蛛池模板: 亚洲一区在线视频 | 国产欧美综合一区二区三区 | 日韩国产精品视频 | 日韩国产在线 | 视频一区二区在线 | 精品视频在线观看免费 | 日韩欧美在线视频观看 | 91午夜理伦私人影院 | 欧美性生交xxxxx | 欧美大片91 | 亚洲视频免费看 | 国产精品一区二 | 国产精自产拍久久久久久蜜 | 国产又粗又大又长 | 日韩黄色免费视频 | 免费一级黄色录像 | 国产福利一区二区三区 | 久久av资源 | 中文字幕在线免费观看 | 欧美日韩激情 | 午夜精品福利视频 | 黄色小视频在线播放 | www.久| 日韩中文字幕在线 | 91色漫| 国产三级视频在线播放 | 欧美裸体视频 | 成人一区二区三区 | 国产成人午夜 | 日韩不卡一区 | 免费一级黄色片 | 久久久久久成人 | 国产一区精品在线观看 | 一区二区在线看 | 黄色一节片 | 色婷婷香蕉在线一区二区 | 国产在线观看一区 | 午夜视频一区二区三区 | 黄色片在线观看视频 | 一区二区国产精品 | 色在线视频 |