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 *
def NoWorko(bitmap):
bmData = bitmap.LockBits(Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
total_bytes = (bmData.Stride) * bmData.Height
rgbValues = Array.CreateInstance(Byte, total_bytes)
Runtime.InteropServices.Marshal.Copy(bmData.Scan0, rgbValues, 0, total_bytes)
for i in rgbValues:
i = 255 - i
#The following line doesn't appear to actually copy the bits back
Runtime.InteropServices.Marshal.Copy(rgbValues, 0, bmData.Scan0, total_bytes)
bitmap.UnlockBits(bmData)
originalImage = Bitmap("Test.bmp")
newPic = NoWorko(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.
Update
My foolishness has been pointed out in trying to add a red tint to a white image - so now the code simply inverts the colours. I've tried this on a number of images, but it just doesn't seem to have any effect.
However, the following (very similar) C# program:
public static void Main(string[] args)
{
Bitmap bitmap = new Bitmap("nTest.jpg");
BitmapData bmData = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int total_bytes = (bmData.Stride) * bmData.Height;
byte[] rgbValues = new byte[total_bytes];
Marshal.Copy(bmData.Scan0, rgbValues, 0, total_bytes);
for(int i = 0; i < total_bytes; i++)
{
rgbValues[i] = (byte)(255 - rgbValues[i]);
}
Marshal.Copy(rgbValues, 0, bmData.Scan0, total_bytes);
bitmap.UnlockBits(bmData);
bitmap.Save("nNew.jpg");
}
Worked on all the images I've tried.
I'm not sure, but it seems to be something to do with the call to:
Runtime.InteropServices.Marshal.Copy(rgbValues, 0, bmData.Scan0, bytes)
in IPY that is causing the problem.