Click here to Skip to main content
       

Graphics

 
GeneralGDI+ - Disposing Pinmembermolcaobarman30-Mar-08 7:14 
GeneralRe: GDI+ - Disposing PinmvpMark Salsbery30-Mar-08 9:40 
GeneralRe: GDI+ - Disposing PinmvpLuc Pattyn30-Mar-08 10:24 
GeneralRe: GDI+ - Disposing Pinmembermolcaobarman1-Apr-08 21:23 
GeneralRe: GDI+ - Disposing PinmvpLuc Pattyn1-Apr-08 23:38 
GeneralRe: GDI+ - Disposing Pinmembermolcaobarman2-Apr-08 0:16 
QuestionSemi Transparent UserControl Pinmembermaxatlis30-Mar-08 0:04 
GeneralRe: Semi Transparent UserControl [modified] PinmemberRob 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+ PinmemberReelix28-Mar-08 0:58 
GeneralRe: Flashy GDI+ PinmvpChristian Graus6-Apr-08 16:30 
QuestionTemplate Matching? Pinmemberjamilkhan00727-Mar-08 21:45 
QuestionHighest quality text using TextRender Pinmemberdanreber18-Mar-08 6:16 
GeneralRe: Highest quality text using TextRender PinmemberRob Smiley18-Apr-08 11:43 
QuestionErasing a line C# PinmemberSubin Raju17-Mar-08 20:32 
GeneralRe: Erasing a line C# PinmvpPete O'Hanlon18-Mar-08 11:33 
QuestionWhat thing is done from IStream::Read to IWMReaderCallbackAdvanced::OnStreamSample? Pinmembermarkliu-codeproject16-Mar-08 22:14 
GeneralCreateDIBitmap with 32bits RGBA data PinmemberVlasta_12-Mar-08 23:02 
GeneralRe: CreateDIBitmap with 32bits RGBA data PinmvpMark Salsbery13-Mar-08 7:32 
GeneralRe: CreateDIBitmap with 32bits RGBA data PinmemberVlasta_13-Mar-08 8:24 
GeneralRe: CreateDIBitmap with 32bits RGBA data PinmvpMark Salsbery13-Mar-08 8:28 
GeneralRe: CreateDIBitmap with 32bits RGBA data PinmvpMichael Dunn13-Mar-08 10:30 
GeneralRe: CreateDIBitmap with 32bits RGBA data PinmemberVlasta_13-Mar-08 10:53 
GeneralRe: CreateDIBitmap with 32bits RGBA data PinmemberVlasta_13-Mar-08 11:00 
GeneralRe: CreateDIBitmap with 32bits RGBA data PinmvpMark Salsbery13-Mar-08 11:25 
GeneralRe: CreateDIBitmap with 32bits RGBA data PinmvpMark Salsbery13-Mar-08 11:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web01 | 2.8.141015.1 | Last Updated 10 Oct 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid