Click here to Skip to main content
15,878,748 members
Home / Discussions / Graphics
   

Graphics

 
GeneralGDI+ - Disposing Pin
molcaobarman30-Mar-08 7:14
molcaobarman30-Mar-08 7:14 
GeneralRe: GDI+ - Disposing Pin
Mark Salsbery30-Mar-08 9:40
Mark Salsbery30-Mar-08 9:40 
GeneralRe: GDI+ - Disposing Pin
Luc Pattyn30-Mar-08 10:24
sitebuilderLuc Pattyn30-Mar-08 10:24 
GeneralRe: GDI+ - Disposing Pin
molcaobarman1-Apr-08 21:23
molcaobarman1-Apr-08 21:23 
GeneralRe: GDI+ - Disposing Pin
Luc Pattyn1-Apr-08 23:38
sitebuilderLuc Pattyn1-Apr-08 23:38 
GeneralRe: GDI+ - Disposing Pin
molcaobarman2-Apr-08 0:16
molcaobarman2-Apr-08 0:16 
QuestionSemi Transparent UserControl Pin
maxatlis30-Mar-08 0:04
maxatlis30-Mar-08 0:04 
GeneralRe: Semi Transparent UserControl [modified] Pin
Rob Smiley18-Apr-08 9:48
Rob Smiley18-Apr-08 9:48 
Hi, the short answer to your question is no. The win32 system does not support semi-transparent controls in the way you want it. However, considering that you're UserControl is painting an image, there is an alternative way - as long as you're only painting over other graphics & not over controls (buttons, textboxes etc.). Any image you want to paint can be adjusted so that it's semi-transparent. Ditch the UserControl & paint the image from the main form by overriding the OnPaint method. Here's the code to adjust the image transparency. You'll need to enable the 'Allow unsafe code' option in the project Build properties.

private Bitmap _transImage = null;

public Form1()
{
    InitializeComponent();
}

~Form1()
{
    _transImage.Dispose();
}

protected override void OnLoad(EventArgs e)
{
    Bitmap b = new Bitmap(@"c:\myImage.png");

    _transImage = AdjustImageTransparency(b);

    b.Dispose();

    base.OnLoad(e);
}

protected override void OnPaint(PaintEventArgs e)
{
    e.Graphics.DrawImage(_transImage, new Rectangle(40, 40, _transImage.Width, _transImage.Height));

    base.OnPaint(e);
}

private Bitmap AdjustImageTransparency(Bitmap sourceImage)
{
    // image must be 32bpp to allow transparency adjustment, so paint source to transImage gc
    Bitmap transImage = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
    Rectangle rc = new Rectangle(Point.Empty, sourceImage.Size);

    using (Graphics g = Graphics.FromImage(transImage))
        g.DrawImage(sourceImage, rc);

    // lock transImage for direct memory manipulation
    BitmapData bd = transImage.LockBits(rc, ImageLockMode.ReadWrite, transImage.PixelFormat);

    AdjustTransparency(bd, 0.5f);

    // must always unlock the memory when finished
    transImage.UnlockBits(bd);

    return transImage;
}

private unsafe void AdjustTransparency(BitmapData dataObj, float ratio)
{
    // calc the number of bytes that represent a single row
    int byteWidth = dataObj.Width * 4;

    for (int y = 0; y < dataObj.Height; y++)
    {
        // calc memory location for row(y)
        byte* row = (byte*)dataObj.Scan0 + dataObj.Stride * y;

        for (int x = 0; x < byteWidth; x++)
        {
            // every 4th byte in a 32bpp image is the alpha value for each pixel
            if ((x % 4) == 3)
            {
                // adjust value of transparency byte according to ratio (0-1 value)
                float val = (float)row[x];
                val *= ratio;
                val = Math.Min(Math.Max(val, 0), 255);

                row[x] = (byte)val;
            }
        }
    }
}


also, add the System.Drawing.Imaging import to your code.

Hope this helps! Big Grin | :-D

Rob

"An eye for an eye only ends up making the whole world blind"

modified on Friday, April 18, 2008 3:58 PM

GeneralFlashy GDI+ Pin
Reelix28-Mar-08 0:58
Reelix28-Mar-08 0:58 
GeneralRe: Flashy GDI+ Pin
Christian Graus6-Apr-08 16:30
protectorChristian Graus6-Apr-08 16:30 
QuestionTemplate Matching? Pin
jamilkhan00727-Mar-08 21:45
jamilkhan00727-Mar-08 21:45 
QuestionHighest quality text using TextRender Pin
danreber18-Mar-08 6:16
danreber18-Mar-08 6:16 
GeneralRe: Highest quality text using TextRender Pin
Rob Smiley18-Apr-08 11:43
Rob Smiley18-Apr-08 11:43 
QuestionErasing a line C# Pin
Denver Thomas17-Mar-08 20:32
Denver Thomas17-Mar-08 20:32 
GeneralRe: Erasing a line C# Pin
Pete O'Hanlon18-Mar-08 11:33
mvePete O'Hanlon18-Mar-08 11:33 
QuestionWhat thing is done from IStream::Read to IWMReaderCallbackAdvanced::OnStreamSample? Pin
markliu-codeproject16-Mar-08 22:14
markliu-codeproject16-Mar-08 22:14 
GeneralCreateDIBitmap with 32bits RGBA data Pin
Vlasta_12-Mar-08 23:02
Vlasta_12-Mar-08 23:02 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Mark Salsbery13-Mar-08 7:32
Mark Salsbery13-Mar-08 7:32 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Vlasta_13-Mar-08 8:24
Vlasta_13-Mar-08 8:24 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Mark Salsbery13-Mar-08 8:28
Mark Salsbery13-Mar-08 8:28 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Michael Dunn13-Mar-08 10:30
sitebuilderMichael Dunn13-Mar-08 10:30 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Vlasta_13-Mar-08 10:53
Vlasta_13-Mar-08 10:53 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Vlasta_13-Mar-08 11:00
Vlasta_13-Mar-08 11:00 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Mark Salsbery13-Mar-08 11:25
Mark Salsbery13-Mar-08 11:25 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Mark Salsbery13-Mar-08 11:31
Mark Salsbery13-Mar-08 11:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.