Checkered background for images with varying Alpha

I

illegal.prime

Hi all, I'm looking for the right way to create a checkered background
in a PictureBox. I will be applying various images to this PictureBox
and I want to to see the checkered background based on the level of
alpha in the image.

I have an algorithm that will traverse a bitmap's pixel and on a pixel
by pixel basis determine the alpha of the image. But my problem is
where to put this algorithm. If I put it in here:
protected override void OnPaintBackground(PaintEventArgs pevent)

it seems to just constantly be invoked whether or not anything is being
done to the image. It may be that the very assignment I'm doing inside
this method is causing it to be reinvoked - i.e. I assign to this.Image
inside this method. Though if that were the case, I would expect the
call stack to grow as time progressed, but it never does.

Any hints as to how to integrate this code into a class that extends
PictureBox? A URL where someone else has done this would also be
useful.

Thanks,
Novice
 
J

jasco4617

I have never done anything like this, but my instict would say you
would want to throw this algorithm into a function and then call the
function when ever you change background images.

I would assume that OnPaintBackground is getting called call constantly
to since you picture is constantly being redrawn for some reason.

So my suggestion would be to run you algorithm on form load and during
any other event where the picture is actually changed (such as a button
click or something).

This is just a stab in the dark though.

- Patrick
 
I

illegal.prime

I've got it working.. kinda...

The other problem is that there are a bunch of controls (an arbitrary
number) that can modify the PictureBox... so I think the right place
for this is the OnPaintBackground or OnPaint. This seems to be
combining things well enough, but now I have two other problems:
1. I get a non-straight line drawn in a seemingly random spot outside
of the PictureBox control... it is so weird... I have no idea how my
OnPaint (within the class that extends PictureBox) would even have
access to that part of the Panel (since it is outside of the
PictureBox).
2. The other problem is that there seems to be some issue with scaling
the image/bitmap I create to the image that belongs to the PictureBox

Anybody know of a publicly accessible C# source code that does this?
link?

Thanks,
Novice
 
I

illegal.prime

Okay more or less solved the problems - turns out in a PictureBox's
OnPaint event handler you HAVE to use the graphics from the event - not
from an image you create. I.E. this is bad
Bitmap resultBMP = new Bitmap(imageWidth, imageHeight);
using (Graphics graphics = Graphics.FromImage(resultBMP))
{
//code
}

This is good:
Graphics graphics = e.Graphics;
//code

For some screwy reason the "bad" code would make a random curved line
(almost looked like a random scratch) appear outside of the control's
constraints which is just nutty!

..Net voodoo/magic

Novice
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top