[C#] 이미지 Fade in 하며 보여주기
제가 만든 소스코드가 아니기 때문에 특별한 설명은 올리지 않겠습니다.
**PlatformAPIs.cs**
****
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace 쓰려고 하는 네임스페이스
{
public struct BlendFunction
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
public enum BlendOperation : byte
{
AC_SRC_OVER = 0x00
}
public enum BlendFlags : byte
{
Zero = 0x00
}
public enum SourceConstantAlpha : byte
{
Transparent = 0x00,
Opaque = 0xFF
}
public enum AlphaFormat : byte
{
AC_SRC_ALPHA = 0x01
}
public class PlatformAPIs
{
[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);
}
}
**DrawAlpha() 함수 **
****
Graphics gxBuffer;
Bitmap offBitmap;
Bitmap slideAni;
slideAni = new Bitmap(“FadeIn 하려는 이미지 경로”);
offBitmap = new Bitmap(this.Width, this.Height);
gxBuffer = Graphics.FromImage(offBitmap);
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()
}
}
**Timer 를 통해서 호출하기 **
****
private void timer1_Tick(object sender, EventArgs e)
{
if (opacity == 252) //0~255
{
timer1.Enabled = false;
this.CreateGraphics().Clear(Color.WhiteSmoke);
}
else
{
gxBuffer.Clear(this.BackColor);
DrawAlpha(gxBuffer, slideAni, opacity, 0, 250);
this.e.DrawImage(offBitmap, 0, 0);
opacity = (byte)(opacity + 6);
}
}