Click here to Skip to main content
       

Graphics

 
GeneralShading Pinmembermeister_skypie6-Apr-08 1:13 
GeneralRe: Shading PinmemberTim Craig6-Apr-08 16:05 
GeneralRe: Shading PinmemberEl Corazon8-Apr-08 7:20 
QuestionPartially redraw Image Pinmemberinvader824-Apr-08 3:31 
GeneralRe: Partially redraw Image PinmvpLuc Pattyn4-Apr-08 3:41 
GeneralRe: Partially redraw Image Pinmemberinvader824-Apr-08 3:50 
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 
GeneralRe: CreateDIBitmap with 32bits RGBA data PinmemberVlasta_13-Mar-08 12:30 
GeneralRe: CreateDIBitmap with 32bits RGBA data PinmvpMark Salsbery13-Mar-08 13:25 
GeneralRe: CreateDIBitmap with 32bits RGBA data PinmemberVlasta_13-Mar-08 14:04 
Generaldirectsound can not be initized Pinmembermwolf12212-Mar-08 16:39 
GeneralControlStyles.DoubleBuffer Issue PinmemberHarveySaayman7-Mar-08 2:12 
GeneralRe: ControlStyles.DoubleBuffer Issue PinmvpLuc Pattyn7-Mar-08 2:20 
GeneralRe: ControlStyles.DoubleBuffer Issue PinmemberHarveySaayman9-Mar-08 20:23 
GeneralRe: ControlStyles.DoubleBuffer Issue PinmvpLuc Pattyn10-Mar-08 0:51 
GeneralRe: ControlStyles.DoubleBuffer Issue PinmemberHarveySaayman10-Mar-08 1:22 
GeneralRe: ControlStyles.DoubleBuffer Issue PinmvpMartin#12-Mar-08 3:43 
Questiondrawing in a rectangle? PinmemberHarveySaayman6-Mar-08 3:35 
GeneralRe: drawing in a rectangle? PinmvpLuc Pattyn6-Mar-08 8:00 
GeneralRe: drawing in a rectangle? PinmemberHarveySaayman6-Mar-08 19:43 
GeneralRe: drawing in a rectangle? PinmemberHarveySaayman6-Mar-08 22:57 
GeneralRe: drawing in a rectangle? PinmvpLuc Pattyn7-Mar-08 0:07 
GeneralRe: drawing in a rectangle? PinmemberHarveySaayman7-Mar-08 0:54 
Generalthis.CreateGraphics() Issue PinmemberHarveySaayman5-Mar-08 21:32 
GeneralRe: this.CreateGraphics() Issue PinmemberTim Craig5-Mar-08 22:33 
GeneralRe: this.CreateGraphics() Issue PinmvpMark Salsbery6-Mar-08 6:06 

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
Web03 | 2.8.141022.1 | Last Updated 10 Oct 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid