c# - Need to clone a bitmap image without locking the original image -


i used below code clone bitmap image without locking original file. facing issue cloned image (.gif) not same original image. especially, color of cloned image not proper.

am doing wrong? better way have image in memory , original file deleted hard disk?

code:

private bitmap cloneimage(bitmap src)     {         if (src == null)             return src;         bitmap bitmap = new bitmap(src.size.width, src.size.height, src.pixelformat);         system.drawing.rectangle bounds = new system.drawing.rectangle(0, 0, src.width, src.height);         system.drawing.imaging.bitmapdata bmpdata = src.lockbits(bounds, system.drawing.imaging.imagelockmode.readwrite, src.pixelformat);         system.drawing.imaging.bitmapdata newbmpdata = bitmap.lockbits(bounds, system.drawing.imaging.imagelockmode.readwrite, src.pixelformat);         intptr bptr = bmpdata.scan0;         intptr nbptr = newbmpdata.scan0;         int bytes = math.abs(bmpdata.stride) * src.height;         byte[] rgbvalues = new byte[bytes];          system.runtime.interopservices.marshal.copy(bptr, rgbvalues, 0, bytes);         system.runtime.interopservices.marshal.copy(rgbvalues, 0, nbptr, bytes);         bitmap.unlockbits(newbmpdata);         src.unlockbits(bmpdata);         return bitmap;     } 

original image:

original image

cloned image:

cloned image

this looks palette issue. individual pixels @ right location, memcpy code right.

either copy palette, or use 24 or 32 bit pixel format , use graphics.fromimage blit source image onto target bitmap. can save png going smaller file anyway.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -