What's the difference between Bitmap.Clone() and new Bitmap(Bitmap)?
It is the common difference between a "deep" and a "shallow" copy, also an issue with the almost-deprecated IClonable interface. The Clone() method creates a new Bitmap object but the pixel data is shared with the original bitmap object. The Bitmap(Image) constructor also creates a new Bitmap object but one that has its own copy of the pixel data.
Lots of questions about Clone() at SO where the programmer hopes that it avoids the typical trouble with bitmaps, the lock on the file from which it was loaded. It doesn't. A possibly practical usage is avoiding trouble with a library method that inappropriately calls Dispose() on a passed bitmap.
The overloads may be useful, taking advantage of the pixel format conversion or the cropping options.
How to create a Bitmap deep copy
B.Clone(new Rectangle(0, 0, B.Width, B.Height), B.PixelFormat)
Difference between bitmap and bitmapdata
System.Drawing.Bitmap
is an actual bitmap object. You can use it to draw to using a Graphics
instance obtained from it, you can display it on the screen, you can save the data to a file, etc.
The System.Drawing.Imaging.BitmapData
class is a helper object used when calling the Bitmap.LockBits()
method. It contains information about the locked bitmap, which you can use to inspect the pixel data within the bitmap.
You can't really "convert" between the two per se, as they don't represent the same information. You can obtain a BitmapData
object from a Bitmap
object simply by calling LockBits()
. If you have a BitmapData
object from some other Bitmap
object, you can copy that data to a new Bitmap
object by allocating one with the same format as the original, calling LockBits
on that one too, and then just copying the bytes from one to the other.
Problem with Bitmap Cloning
You could also load the bitmap without file locking using the simple workaround:
using (Stream s = File.OpenRead(@"\My Documents\My Pictures\Waterfall.jpg"))
Bitmap _backImage = (Bitmap)Bitmap.FromStream(s);
Bitmap lockbits and cloning
I think Bitmap.Clone()
does not make a deep copy and the data is shared.
Edit: Following the advice given below, move the clone line just after var b
and make it like this: var clone = new Bitmap(b);
. It works now.
Related Topics
How to Compare Two Rich Text Box Contents and Highlight the Characters That Are Changed
How to Make Multi-Language App in Winforms
Using the Iterator Variable of Foreach Loop in a Lambda Expression - Why Fails
Redirect from Action Filter Attribute
C# .Equals(), .Referenceequals() and == Operator
How to Quickly Check If Two Data Transfer Objects Have Equal Properties in C#
Parsing Ftpwebrequest Listdirectorydetails Line
Oledbparameters and Parameter Names
Sorting a List Using Lambda/Linq to Objects
Logging Request/Response Messages When Using Httpclient
How Enumerate All Classes with Custom Class Attribute
Escape Special Character in Regex
Export to Excel from a List with Epplus
Viewmodels in MVC/Mvvm/Separation of Layers- Best Practices
Why Must a Lambda Expression Be Cast When Supplied as a Plain Delegate Parameter