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:
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
Post a Comment