Using Bitmap.LockBits and Marshal.Copy in IronPython not changing image as expected
- by Leonard H Martin
Hi all,
I have written the following IronPython code:
import clr
clr.AddReference("System.Drawing")
from System import *
from System.Drawing import *
from System.Drawing.Imaging import *
originalImage = Bitmap("Test.bmp")
def RedTint(bitmap):
bmData = bitmap.LockBits(Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
ptr = bmData.Scan0
bytes = bmData.Stride * bitmap.Height
rgbValues = Array.CreateInstance(Byte, bytes)
Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
for i in rgbValues[::3]:
i = 255
Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)
bitmap.UnlockBits(bmData)
return bitmap
newPic = RedTint(originalImage)
newPic.Save("New.bmp")
Which is my interpretation of this MSDN code sample: http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx except that I am saving the altered bitmap instead of displaying it in a Form.
The code runs, however the newly saved bitmap is an exact copy of the original image with no sign of any changes having occurred (it's supposed to create a red tint). Could anyone advise what's wrong with my code?
The image I'm using is simply a 24bpp bitmap I created in Paint (it's just a big white rectangle!), using IronPython 2.6 and on Windows 7 (x64) with .Net Framework 3.5 SP1 installed.