Skip to content

C# working .with .GIF in transparent form

John Lester Almenanza

Asked Mar 21, 2015, 8:21 PM EDT1 answer
Share
LinkedInX
How can I make a gif move in a transparent form???
 
I have tried some codes from the internet but it just make my form to be transparent. My gif just turned to be static.
//Download from
//Visual C# Kicks - http://www.vcskicks.com/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //Needed
namespace SplashScreen
{
public partial class frmSplash : Form
{
public frmSplash()
{
InitializeComponent();
}
#region CUSTOM PAINT METHODS ----------------------------------------------
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00080000; // Required: set WS_EX_LAYERED extended style
return cp;
}
}
//Updates the Form's display using API calls
public void UpdateFormDisplay(Image backgroundImage)
{
IntPtr screenDc = API.GetDC(IntPtr.Zero);
IntPtr memDc = API.CreateCompatibleDC(screenDc);
IntPtr hBitmap = IntPtr.Zero;
IntPtr oldBitmap = IntPtr.Zero;
try
{
//Display-image
Bitmap bmp = new Bitmap(backgroundImage);
hBitmap = bmp.GetHbitmap(Color.FromArgb(0)); //Set the fact that background is transparent
oldBitmap = API.SelectObject(memDc, hBitmap);
//Display-rectangle
Size size = bmp.Size;
Point pointSource = new Point(0, 0);
Point topPos = new Point(this.Left, this.Top);
//Set up blending options
API.BLENDFUNCTION blend = new API.BLENDFUNCTION();
blend.BlendOp = API.AC_SRC_OVER;
blend.BlendFlags = 0;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = API.AC_SRC_ALPHA;
API.UpdateLayeredWindow(this.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, API.ULW_ALPHA);
//Clean-up
bmp.Dispose();
API.ReleaseDC(IntPtr.Zero, screenDc);
if (hBitmap != IntPtr.Zero)
{
API.SelectObject(memDc, oldBitmap);
API.DeleteObject(hBitmap);
}
API.DeleteDC(memDc);
}
catch (Exception)
{
}
}
#endregion
#region MENU EVENTS -------------------------------------------------------
private void mHomepage_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.vcskicks.com/");
}
private void mExit_Click(object sender, EventArgs e)
{
this.Close(); //no extra commands are required
}
#endregion
#region FORM EVENTS -------------------------------------------------------
private void Form1_Load(object sender, EventArgs e)
{
UpdateFormDisplay(this.BackgroundImage);
}
protected override void OnPaint(PaintEventArgs e)
{
//Call our drawing function
UpdateFormDisplay(this.BackgroundImage);
}
#endregion
}
internal class API
{
public const byte AC_SRC_OVER = 0x00;
public const byte AC_SRC_ALPHA = 0x01;
public const Int32 ULW_ALPHA = 0x00000002;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BLENDFUNCTION
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

[DllImport("user32.dll", ExactSpelling = true)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern bool DeleteDC(IntPtr hdc);

[DllImport("gdi32.dll", ExactSpelling = true)]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern bool DeleteObject(IntPtr hObject);
}
}
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I took a look at your code. You're not cycling through the GIF pages and painting those. There's not a single loop in your application here, and the painting only seems to ever render static data. Therefore, you only see one image.

    If you are trying to create animated GIF images, Leadtools is a good programming toolkit that supports creating animated GIF images. I found the following post that might help:
    http://support.leadtools.com/SupportPortal/CS/forums/17903/showpost.aspx
    0