Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Bitmap.LockBits and Marshal.Copy in IronPython not changing image as expected
    text
    copied!<p>I have written the following IronPython code:</p> <pre><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") </code></pre> <p>Which is my interpretation of this MSDN code sample: <a href="http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx</a> except that I am saving the altered bitmap instead of displaying it in a Form.</p> <p>The code runs, however the newly saved bitmap is an exact copy of the original image with no sign of any changes having occurred <strike><strong>(it's supposed to create a red tint)</strong></strike>. Could anyone advise what's wrong with my code?</p> <p><strike><strong>The image I'm using is simply a 24bpp bitmap I created in Paint (it's just a big white rectangle!)</strong></strike>, using IronPython 2.6 and on Windows 7 (x64) with .Net Framework 3.5 SP1 installed.</p> <p><strong>Update</strong></p> <p>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.</p> <p>However, the following (very similar) C# program:</p> <pre><code>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 &lt; total_bytes; i++) { rgbValues[i] = (byte)(255 - rgbValues[i]); } Marshal.Copy(rgbValues, 0, bmData.Scan0, total_bytes); bitmap.UnlockBits(bmData); bitmap.Save("nNew.jpg"); } </code></pre> <p>Worked on all the images I've tried.</p> <p>I'm not sure, but it seems to be something to do with the call to:</p> <p>Runtime.InteropServices.Marshal.Copy(rgbValues, 0, bmData.Scan0, bytes)</p> <p>in IPY that is causing the problem. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload