• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1029
  • Last Modified:

AlphaBlend with CF2

Hi,

I try to create an CustomControl with CF2 but it ever just show an grey field and I can't understand why...
I include the source code here while the AlphaBlend is a P/Invoke from the core.dll:
 [DllImport("coredll.dll")]
 extern public static Int32 AlphaBlend(IntPtr hdcDest, Int32 xDest, Int32 yDest, Int32 cxDest, Int32 cyDest, IntPtr hdcSrc, Int32 xSrc, Int32 ySrc, Int32 cxSrc, Int32 cySrc, BlendFunction blendFunction);

Maybe somebody can show me what I'm doing wrong ...

Thanks,

Andre
using System;
 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace ctrlDevice
{
    public partial class tButton : Control
    {
        public event EventHandler onClick;
 
        Bitmap _image;
        Bitmap _hover;
 
        protected virtual void DoClick(EventArgs e)
        {
            EventHandler _onClick = this.onClick;
            if (_onClick != null)
            {
                onClick(this, e);
            }
        }
 
        public Bitmap Image
        {
            get { return _image; }
            set
            {
                _image = value;
            }
        }
 
        public Bitmap HoverImage
        {
            get { return _hover; }
            set { _hover = value; }
        }
 
        public tButton()
        {
            InitializeComponent();
        }
 
        protected override void OnPaint(PaintEventArgs pe)
        {
            Graphics g = pe.Graphics;
            DrawAlpha(gx, _image, 0, this.Location.X, this.Location.Y);
            // Calling the base class OnPaint
            // base.OnPaint(pe);
        }
 
        private void DrawAlpha(Graphics gx, Bitmap image, byte transp, int x, int y)
        {
            using (Graphics gxSrc = Graphics.FromImage(image))
            {
                IntPtr hdcDst = gx.GetHdc();
                IntPtr hdcSrc = gxSrc.GetHdc();
                BlendFunction blendFunction = new BlendFunction();
                blendFunction.BlendOp = (byte)BlendOperation.AC_SRC_OVER;   // Only supported blend operation
                blendFunction.BlendFlags = (byte)BlendFlags.Zero;           // Documentation says put 0 here
                blendFunction.SourceConstantAlpha = transp;// Constant alpha factor
                blendFunction.AlphaFormat = (byte)0;                        // Don't look for per pixel alpha
                PlatformAPIs.AlphaBlend(hdcDst, x, y, image.Width, image.Height, hdcSrc, 0, 0, image.Width, image.Height, blendFunction);
                gx.ReleaseHdc(hdcDst);    // Required cleanup to GetHdc()
                gxSrc.ReleaseHdc(hdcSrc);       // Required cleanup to GetHdc()
            }
        }
    }
}

Open in new window

0
andre72
Asked:
andre72
  • 7
  • 4
1 Solution
 
jgordosCommented:
In your call to DrawAlpha, you're doing this....

DrawAlpha(gx, _image, 0, this.Location.X, this.Location.Y);

Per the declaration of the routine,
private void DrawAlpha(Graphics gx, Bitmap image, byte transp, int x, int y)

that means you're passing in a zero byte for your "trans"...  I *think* the value should iterate over the interval 0x00 to 0xFF, so the zero you're passing is 'completely transparent', and 0xFF would be fully opaque.

As a quick hack try... change the zero from 0 to 128 on your call, and see if you get a blended image.

-john




0
 
andre72Author Commented:
Thanks, good idea - but I tried it out and the problem is the same ...
DrawAlpha(g, _image, 128, this.Location.X, this.Location.Y);
0
 
andre72Author Commented:
Args, for sure Location.X & Location.Y was outside the control ... 0,0 works fine ...
But however not what I need - also all aroud my PNG with alpha-channel is grey ...
Does anybody have an idea?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jgordosCommented:
What?

Does this mean the alpha is working now?

I'm confused!

-john

0
 
jgordosCommented:
What?

Does this mean the alpha is working now?

I'm confused!

-john

0
 
jgordosCommented:
I'm so confused I've started double posting!!
0
 
andre72Author Commented:
I was coinfused too when I wrote the posting ...
Yes, the blit works and I think it works from the beginning, it had just been outside the visible place.

However in the background of my picture is ever the gray color of the control,
Not really useful for an transparent display ...

Is it possible to make it "real" transparent?
0
 
jgordosCommented:
I'm a bit confused by what you mean, now....

The background of your picture is... what?  Like in this picture....


soccer-trans.jpg
0
 
jgordosCommented:
is the magenta color around the ball what you mean by "background"?

If so, and you're seeing the grey of the control.. then, I'd say it's working correctly.

Is this what you're seeing?

-john
0
 
andre72Author Commented:
Yes you're right it's wroking correctly, it is transparent in itself.
And I think there's no way to make a real transparent control for any background (I mean the controls behind my own one) or is there a way to have no gray background?

If not I think an other solution would be to copy the background and than blit the own image on it.
Is there a way for to do so as I see that ParentForm property is not available.

Andre
0
 
jgordosCommented:
That's a different topic.

But yes, you can have alpha blended dialog box controls... It's all up to how ambitious you want to get with replacing some of the standard windows controls, and their paint methods.

People do this for rounded window corners, for example.. like in solitaire (the cards are windows).
http://blogs.msdn.com/llobo/archive/2006/03/09/543762.aspx

Or say, a non-rectangular windows splash screen...
http://blogs.msdn.com/llobo/archive/2006/02/07/Creating-NonRectangular-Splash-Screen-using-Alpha-Channel.aspx

-john
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now