-
Magick.NET version: Magick.NET-Q16-x64 14.10.3Environment: Windows 11 26200.7922/.NET 10.0/C# 14.0I am trying to create an independent copy of a MagickImage to perform pixel modifications using GetPixelsUnsafe(). But I noticed that modifying the cloned image also changes the original image. I expected Clone() or the copy constructor to create a fully separate copy, but it seems they still share the same underlying pixel buffer in memory. using (MagickImage cloneBefore = new("lena_std.tif"))
using (var cloneAfter = (MagickImage)cloneBefore.Clone()) {
HalvePixelValue(cloneAfter);
cloneBefore.Write("lena_std_clone_before.tif");
cloneAfter.Write("lena_std_clone_after.tif");
}
using (MagickImage newBefore = new("lena_std.tif"))
using (MagickImage newAfter = new(newBefore)) {
HalvePixelValue(newAfter);
newBefore.Write("lena_std_new_before.tif");
newAfter.Write("lena_std_new_after.tif");
}
static unsafe void HalvePixelValue(MagickImage img) {
var (w, h, ch) = (img.Width, img.Height, img.ChannelCount);
using var pixels = img.GetPixelsUnsafe();
var ptr = (ushort*)pixels.GetAreaPointer(0, 0, w, h);
for (var i = 0; i < w * h * ch; i++) ptr[i] = (ushort)(ptr[i] / 2); // just demo, not the actual operation
}Could you please clarify if this is expected behavior? How can I ensure the image has its own isolated pixel buffer before using GetPixelsUnsafe()? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Update: Possible solution found using var cloneAfter = (MagickImage)cloneBefore.CloneArea(cloneBefore.Width, cloneBefore.Height); |
Beta Was this translation helpful? Give feedback.
-
|
This is actually by design but should probably be better documented. When you perform a |
Beta Was this translation helpful? Give feedback.
This is actually by design but should probably be better documented. When you perform a
Clonewithout any arguments you will reference the pixels of the other image until you change them. But because you are asking for direct access to the pixels you will get access to the pixels that are now referenced by both images. Your method is the way to work around this.