Displaying transparent images

L

Lonifasiko

All images of my application have transparent background color. I think
it's the normal approach. My application is divided into 4 different
parts, and each part is going to be displayes in one color. Some forms
will go blue, yellow, green or red.

What about displaying the images with transparent background? I only
can see a white annoying background in the picturebox controls I use to
display mi images.

I've searched the group for answers but can't find anything cristal
clear enough.

Could anybody point me in the right direction? Any possible solution,
even commercial controls?

I've read CF does not support transparent images.......I can't believe
my eyes! Is this real?

Thnaks very much in advance.
 
J

Joseph Byrns

I'd guess you are using the wrong colour for the transparency. Do you have
an example you can post?
 
L

Lonifasiko

Hi Joseph, thanks for your interest.

Following Sergey Bogdanov's advice, I've create a class that inherits
from PictureBox. It only overrides two methods. This is the code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;

namespace ColorinColorau
{
public class CustomPictureBox: System.Windows.Forms.PictureBox
{
protected override void
OnPaint(System.Windows.Forms.PaintEventArgs e)
{
ImageAttributes attr = new ImageAttributes();
attr.SetColorKey(Color.FromArgb(255, 0, 255),
Color.FromArgb(255, 0, 255));
e.Graphics.DrawImage(this.Image, this.ClientRectangle, 0,
0,
this.Image.Width, this.Image.Height, GraphicsUnit.Pixel,
attr);
}

protected override void
OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
{
SolidBrush sb = new SolidBrush(Parent.BackColor);
e.Graphics.FillRectangle(sb, this.ClientRectangle);
}
}
}

Then, in the designer of my form:

//declare the custom picturebox
public CustomPictureBox pictureBox4;

// instantiate it
this.pictureBox4 = new ColorinColorau.CustomPictureBox();

// configure some properties
//

this.pictureBox4.Image =
((System.Drawing.Image)(resources.GetObject("pictureBox4.Image")));
this.pictureBox4.Location = new System.Drawing.Point(4, 14);
this.pictureBox4.Name = "pictureBox4";
this.pictureBox4.Size = new System.Drawing.Size(61, 49);
this.pictureBox4.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.CenterImage;

//and finally add it to the form:
this.Controls.Add(pictureBox4);

The background f the image is still white and I can assure you the
image is transparent.

If you have any idea or solution, please let me know.

Regards.
 
J

Joseph Byrns

in the example you have:
Color.FromArgb(255, 0, 255)

this makes the colour (255,0,255) transparent in any image drawn to that
picturebox, (I don't know what colour that is but it's a mix of red and
blue). If you want white to be the transparent colour then this should be:
Color.FromArgb(255, 255, 255), but you should make sure your background
colour is pure white, otherwise it won't work again.

Just to try and clarify here, the CF does not recognise transparencies in
existing images, but what you can do is draw an image to a picturebox and
set a colour of your choice to be transparent when you draw it, which is
what is happening in this example.
 
L

Lonifasiko

Hi Joseph,

Thanks very much for you deep but concious explanation.

I just copied the code snippet that Sergey Bogdanov suggested in the
post I mentioned you and he was using 255, 0, 255.
Just changed to you 255,255,255 and worked. Now my pictureboxex are
transparent and I can see properly the background color of the form.

Many thanks Joseph for pointing me towards the solution so easily.
Is this (inheriting PictureBox and overriding these two methods), the
only way to achieve it? Is this the normal approach used by most of the
CF programmers?
I thougth PictureBoxEx from OpenNETCF would do it, this way not having
me to code another class inheriting and so on, but same problem occurs.

Thanks very much. Regards.
 
J

Joseph Byrns

This seems a good way of doing it to me.

If you don't want to create a new inhertied pictureBox you could just draw
the image in the OnPaint event of a standard PictureBox. Doing it this way
you just add your image to the assembly rather than assigning it as the
background image, then in the OnPaint of the standard PictureBox draw the
image to the picturebox, just as it is done in the example. The
OnPaintBackground function just paints the background with the form colour,
so this could have been done setting the background colour property in the
designer.

I am not familiar with PictureBoxEx OpenNetCF
 
L

Lonifasiko

Where should I put the OnPaint event? I cannot see any OnPaint event
for normal PictureBox.
If i just add it to the form this way:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
ImageAttributes attr = new ImageAttributes();
attr.SetColorKey(Color.FromArgb(255, 255, 255),
Color.FromArgb(255, 255, 255));
e.Graphics.DrawImage(this.pictureBox4.Image,
this.ClientRectangle, 0, 0,
this.pictureBox4.Image.Width,
this.pictureBox4.Image.Height, GraphicsUnit.Pixel, attr);
}

I would be overriding the OnPaint method of the form. Any ideas?

Today is not definitively my day :(
 
L

Lonifasiko

Yes, I noticed PictureBox in CF 2.0 didn't have OnPaint event, that's
why I told Joseph where to put the code he suggested.

But I'm afraid the unique way of achieving my purpose is creating a
class that inherits from PictureBox. Well, works and grows the amount
of reusable code for other projects.

Regards.
 

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