XORing using vb.net

Hi Michael,
I'm hoping you can help me out here. I've written a graphics app. One of the things I want to do is rubber banding. I know that GD+ and Vb.Net don't support this. So I've written my own sw to do this. The thing is that it works ok with jpgs newly created in MS Paint and my app and some d/l from the Net. It also works with most PNGs. BUT it does NOT work with jpgs from DSLR cameras from Nikon, Canon and Fujifilm.
I don't know why. It seems that VB.Net's functions for reading and writing colours to jpgs isn't working right for jpgs from DSLRs or from my scanner. My actual drawing code works - i.e. it properly draws circles, rectangles, etc. BUT the XORing doesn't work right on many images as I've described.

Yippeeee I've solved it :) Had a sleep and a brainwave lol The problem was I expected all the bitmaps to be 32bpp ones. They aren't. So all I've had to do to make it work is change my test form so that I create a new tempbmp to load the image in. Then I recreate the currentbitmap as a 32bpp bitmap of the same size as the new tempbmp and draw the tempbmp onto the new currentbmp and dispose of the tempbmp. Then everything works as it should! Even the (now 32bpp) copies of DSLR images. I was pulling my hair out wondering what was wrong with my XOR plot routine. It wasn't the plot routine - it was because the image wasn't 32bpp. So problems now solved guys. Thanks anyway. Allan

Help the community by fixing grammatical or spelling errors, summarizing or clarifying the solution, and adding supporting information or resources. Always respect the original author.

I've used GDI+ quite a bit, for a RAW (Nikon) image software package
that I wrote. I may be able to help. However, I do need to know a
couple things. What do you mean that certain PNGs and JPGs "don't
work"? What is it not doing? Are there errors? Does it appear to be
dependent in any way to the resolution of the image?

Hi Chris,
Thanks for replying :)
On the images from my cameras and scanner, points are plotted but not erased when re-plotted despite the XORing. I've called the XOR point/line/rectangle/circle functions exactly the same as the first time I called them.
I'll show the calling code from a test Form. Now, it does work with new blank bitmaps, some jpgs (created with MS Paint) and some d/l from the net (or at least extracted in MS paint from screenshots) plus some I created with an app I've written.
But when I use the exact same code on jpgs from my cameras or scanner, like I've said, it draws the XOR point/line/rectangle/circle (whichever routine I call) - but when I re-call the routine it doesn't erase them - so I'm still left with whatever I've drawn - which is no good for doing rubber-banding etc. Note that the example test form isn't doing any rubber-banding - it simply draws some XORed shapes and then tries erasing them by calling the same XORed shapes in exactly the same way. The shapes are drawn on the jpgs but DON'T get erased on a redraw - and for the life of me I can't figure out why. I don't see why/how image size/resolution matters. BTW the reason I wrote the XOR stuff is because I was doing the rubber-banding using several bitmaps - e.g. an original bitmap, a temp bitmap and a current bitmap and swapping/updating them but while that method is fine for small images, you soon run out memory with images from a DSLR and other large images. So I figured, ok I'll try using XORing - hence I wrote the module you've seen and the test form - (code given below).

My test form code is:

Option Explicit On
Option Strict On
Imports System.Threading

Public Class Form1
'Note: The XORed graphics routines are deliberately NOT anti-aliased
'so if anti-aliasing is needed you have to do it yourself using XORed single pixel points!!!!
Public currentbitmap As New Bitmap(GetScreenWidth, GetScreenHeight)
Dim gr As Graphics = Graphics.FromImage(currentbitmap)
Private filename As String = ""
Private Sub CloseToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CloseToolStripMenuItem.Click
Me.Close()
End Sub

Private Sub NewToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles NewToolStripMenuItem.Click
'For some strange reason, doesn't work unless we get rid of the graphics object and bitmap here first
PictureBox1.Image = Nothing 'Get rid of any current image in the picturebox
gr.Dispose() 'Get rid of the current gr object
currentbitmap.Dispose() 'Get rid of the current (i.e. old) bitmap
currentbitmap = New Bitmap(GetScreenWidth, GetScreenHeight) 'Create a new bitamp
gr = Graphics.FromImage(currentbitmap) 'Create a new graphics object for the new bitmap
gr.Clear(Color.White) 'For some strange reason, doesn't work unless we clear the background here
PictureBox1.Image = currentbitmap
End Sub
End Class

Yippeeee I've solved it :)
Had a sleep and a brainwave lol
The problem was I expected all the bitmaps to be 32bpp ones. They aren't.
So all I've had to do to make it work is change my test form so that I create a new tempbmp to load the image in.
Then I recreate the currentbitmap as a 32bpp bitmap of the same size as the new tempbmp and draw the tempbmp onto the new currentbmp and dispose of the tempbmp.
Then everything works as it should! Even the (now 32bpp) copies of DSLR images.
I was pulling my hair out wondering what was wrong with my XOR plot routine. It wasn't the plot routine - it was because the image wasn't 32bpp.
So problems now solved guys. Thanks anyway.
Allan