Click here to Skip to main content
12,758,605 members (34,231 online)
   

Graphics

 
GeneralGDI+ - Disposing Pin
molcaobarman30-Mar-08 8:14
membermolcaobarman30-Mar-08 8:14 
GeneralRe: GDI+ - Disposing Pin
Mark Salsbery30-Mar-08 10:40
mvpMark Salsbery30-Mar-08 10:40 
GeneralRe: GDI+ - Disposing Pin
Luc Pattyn30-Mar-08 11:24
mvpLuc Pattyn30-Mar-08 11:24 
GeneralRe: GDI+ - Disposing Pin
molcaobarman1-Apr-08 22:23
membermolcaobarman1-Apr-08 22:23 
GeneralRe: GDI+ - Disposing Pin
Luc Pattyn2-Apr-08 0:38
mvpLuc Pattyn2-Apr-08 0:38 
GeneralRe: GDI+ - Disposing Pin
molcaobarman2-Apr-08 1:16
membermolcaobarman2-Apr-08 1:16 
QuestionSemi Transparent UserControl Pin
maxatlis30-Mar-08 1:04
membermaxatlis30-Mar-08 1:04 
GeneralRe: Semi Transparent UserControl [modified] Pin
Rob Smiley18-Apr-08 10:48
memberRob Smiley18-Apr-08 10: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 1:58
memberReelix28-Mar-08 1:58 
GeneralRe: Flashy GDI+ Pin
Christian Graus6-Apr-08 17:30
mvpChristian Graus6-Apr-08 17:30 
QuestionTemplate Matching? Pin
jamilkhan00727-Mar-08 22:45
memberjamilkhan00727-Mar-08 22:45 
QuestionHighest quality text using TextRender Pin
danreber18-Mar-08 7:16
memberdanreber18-Mar-08 7:16 
GeneralRe: Highest quality text using TextRender Pin
Rob Smiley18-Apr-08 12:43
memberRob Smiley18-Apr-08 12:43 
QuestionErasing a line C# Pin
Subin Raju17-Mar-08 21:32
memberSubin Raju17-Mar-08 21:32 
GeneralRe: Erasing a line C# Pin
Pete O'Hanlon18-Mar-08 12:33
mvpPete O'Hanlon18-Mar-08 12:33 
QuestionWhat thing is done from IStream::Read to IWMReaderCallbackAdvanced::OnStreamSample? Pin
markliu-codeproject16-Mar-08 23:14
membermarkliu-codeproject16-Mar-08 23:14 
GeneralCreateDIBitmap with 32bits RGBA data Pin
Vlasta_13-Mar-08 0:02
memberVlasta_13-Mar-08 0:02 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Mark Salsbery13-Mar-08 8:32
mvpMark Salsbery13-Mar-08 8:32 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Vlasta_13-Mar-08 9:24
memberVlasta_13-Mar-08 9:24 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Mark Salsbery13-Mar-08 9:28
mvpMark Salsbery13-Mar-08 9:28 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Michael Dunn13-Mar-08 11:30
mvpMichael Dunn13-Mar-08 11:30 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Vlasta_13-Mar-08 11:53
memberVlasta_13-Mar-08 11:53 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Vlasta_13-Mar-08 12:00
memberVlasta_13-Mar-08 12:00 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Mark Salsbery13-Mar-08 12:25
mvpMark Salsbery13-Mar-08 12:25 
GeneralRe: CreateDIBitmap with 32bits RGBA data Pin
Mark Salsbery13-Mar-08 12:31
mvpMark Salsbery13-Mar-08 12: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.


Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 22 Feb 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid