- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
- [C#] Thiết lập dấu (,) hay dấu (.) ở định dạng số đúng với định dạng số Việt Nam
- [C#] Chia sẻ source code Game Spin Lucky Wheel
- [C#] Hướng dẫn Encode and Decode HTML
- Danh sách tài khoản ChatGPT miễn phí - Hướng dẫn tạo tài khoản Chat Open AI GPT tại Việt Nam
- [C#] Hướng dẫn thay đổi giao diện ứng dụng Winform theo giao diện của Windows
- [VB.NET] Hiệu ứng Acrylic, Mica, Tabbed Blur Effect trên Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên Combobox Edit
- [C#] Chia sẻ source code Orange Rain in Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên XtraMessageBox Winform Devexpress 22.2.3
- [DEVEXPRESS] Hướng dẫn sử dụng HTML and CSS Code Viewer trên Winform
- [C#] Number Effect Counter up and down in winform
- [C#] Hướng dẫn Supend and Resume Process ID in Winform
- [C#] Hiển thị line number trên Richtextbox Winform
- [C#] Fake Blue Screen BSOD in winform
- [C#] Chia sẽ code demo sử dụng Async Parallel Foreach and For in Winform
- [C#] Sử dụng ActionBlock run X task at time winform
- [C#] Hướng dẫn sử dụng Property Grid để lưu và tải lại thông tin cấu hình user trên winform
Tạo hiệu ứng chuyển hình ảnh - Thumb Rotator
Bài viết hôm nay mình thay đổi không khí 1 xíu, mình sẽ giới thiệu để các bạn bài về tạo hiệu ứng chuyển ảnh xoay tròn giống JavaScript cho web. Các bạn xem demo bên dưới để hiểu rõ thêm nha
Sau đây là code của chương trình
public partial class Form1 : Form
{
float m_Alfa = 0;
ThumbElement m_Selected = null;
float m_AlfaAccel = 1;
float m_Perspective= 4;
double PI_FACT = Math.PI / 180.0f;
List m_arImages = new List();
List m_arFO=new List();
public Form1()
{
InitializeComponent();
this.SetStyle(
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer, true);
LoadThumbs();
m_Timer.Interval = 30;
m_Timer.Tick += new EventHandler(m_Timer_Tick);
m_Timer.Enabled = true;
}
void m_Timer_Tick(object sender, EventArgs e)
{
m_Alfa += m_AlfaAccel;
if (m_Alfa > 360)
m_Alfa = m_Alfa - 360;
if (m_Alfa < 0)
m_Alfa = m_Alfa + 360;
//
int maxDX = Bounds.Width/2;
int maxDY = Bounds.Height/2;
for (int i = 0; i < m_arFO.Count; i++)
{
FlyingObject f = m_arFO[i];
Rectangle rc;
int newWidth = 0;
int newHeight = 0;
if (newWidth*f.m_bmpFLyingBitmap.Height/f.m_bmpFLyingBitmap.Width> maxDY)
{
newWidth = maxDX;
newHeight = newWidth * f.m_bmpFLyingBitmap.Height / f.m_bmpFLyingBitmap.Width;
}
else
{
newHeight= maxDY;
newWidth = newHeight * f.m_bmpFLyingBitmap.Width/ f.m_bmpFLyingBitmap.Height;
}
rc = new Rectangle(Bounds.Width / 2 - newWidth / 2,
Bounds.Height / 2 - newHeight / 2,
newWidth,
newHeight);
switch (f.m_nFBState)
{
case 0:
{
f.m_rcFB = new Rectangle(
f.m_rcFBStart.X + (rc.X - f.m_rcFBStart.X) * f.m_nFBPerc / 100,
f.m_rcFBStart.Y + (rc.Y - f.m_rcFBStart.Y) * f.m_nFBPerc / 100,
f.m_rcFBStart.Width + (rc.Width - f.m_rcFBStart.Width) * f.m_nFBPerc / 100,
f.m_rcFBStart.Height + (rc.Height - f.m_rcFBStart.Height) * f.m_nFBPerc / 100
);
f.m_nFBPerc += 5;
if (f.m_nFBPerc > 100)
{
f.m_nFBPerc = 100;
f.m_nFBState = 0;
}
}
break;
case 1:
break;
case 2:
{
f.m_rcFB = new Rectangle(
f.m_rcFB.X + (f.m_tRef.m_Rect.X - f.m_rcFB.X) * (100 - f.m_nFBPerc) / 100,
f.m_rcFB.Y + (f.m_tRef.m_Rect.Y - f.m_rcFB.Y) * (100 - f.m_nFBPerc) / 100,
f.m_rcFB.Width + (f.m_tRef.m_Rect.Width - f.m_rcFB.Width) * (100 - f.m_nFBPerc) / 100,
f.m_rcFB.Height + (f.m_tRef.m_Rect.Height - f.m_rcFB.Height) * (100 - f.m_nFBPerc) / 100
);
f.m_nFBPerc -= 5;
if (f.m_nFBPerc < 0)
{
m_arFO.RemoveAt(i);
i--;
}
}
break;
}
}
Refresh();
}
Timer m_Timer = new Timer();
void LoadThumbs()
{
String[] arStrings=Directory.GetFiles(Application.StartupPath+"Image");
FormProgress pb = new FormProgress();
pb.Show();
for (int i = 0; i < arStrings.Length; i++)
{
pb.progressBar1.Value= (i+1) * 100 / arStrings.Length;
pb.Refresh();
m_arImages.Add(new ThumbElement(arStrings[i]));
}
pb.Visible=false;
pb.Dispose();
pb = null;
for (int i = 0; i < m_arImages.Count; i++)
m_arImages[i].m_dAngleOriginal = (float)i * (360.0f) / (float)m_arImages.Count;
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//base.OnPaintBackground(e);
}
protected override void OnPaint(PaintEventArgs e)
{
Color c=Color.FromArgb(255,0,0,0);
e.Graphics.FillRectangle(new SolidBrush(c),0,0,Width,Height);
int x0 = Width / 2;
int y0 = Height / 2;
int nRadX = (Width / 2) * 7 / 10;
int nRadY = (int)(nRadX / m_Perspective);
for (int i = 0; i < m_arImages.Count; i++)
{
ThumbElement t = m_arImages[i];
m_arImages[i].m_dAngleActual = (m_arImages[i].m_dAngleOriginal + m_Alfa)*PI_FACT;
m_arImages[i].m_dDistanceFromScreen = 10 + 10 * Math.Cos(m_arImages[i].m_dAngleActual);
int x = x0 + (int)(nRadX * Math.Sin(t.m_dAngleActual));
int y = y0 - (int)(nRadY * Math.Cos(t.m_dAngleActual));
float dSize = (float)(80 - 20 * Math.Cos(t.m_dAngleActual));
dSize = dSize / 100;
t.m_Rect.X = (int)(x - (t.m_bmpMain.Width * dSize) / 2);
t.m_Rect.Y = (int)(y - (t.m_bmpMain.Height * dSize));
t.m_Rect.Width = (int)(t.m_bmpMain.Width * dSize);
t.m_Rect.Height = (int)(t.m_bmpMain.Height * dSize);
t.m_RectShadow.X = (int)(x - (t.m_bmpMain.Width * dSize) / 2);
t.m_RectShadow.Y = (int)y;
t.m_RectShadow.Width = (int)(t.m_bmpMain.Width * dSize);
t.m_RectShadow.Height = (int)(t.m_bmpMain.Height * dSize);
}
m_arImages.Sort(delegate(ThumbElement p1, ThumbElement p2) { return p2.m_dDistanceFromScreen.CompareTo(p1.m_dDistanceFromScreen); });
int nIndex = -1;
Point ptClient = PointToClient(MousePosition);
for (int i = m_arImages.Count - 1; i >= 0 && nIndex==-1; i--)
{
ThumbElement t = m_arImages[i];
if (t.m_Rect.Contains(ptClient))
nIndex=i;
}
m_Selected = null;
if (nIndex!=-1)
m_Selected = m_arImages[nIndex];
for (int i = 0; i < m_arImages.Count; i++)
{
ThumbElement t=m_arImages[i];
float dTrasp = (float)(100 + 100 * Math.Cos(t.m_dAngleActual));
e.Graphics.DrawImage(t.m_bmpMain, t.m_Rect);
e.Graphics.DrawImage(t.m_bmpShadow, t.m_RectShadow);
if (nIndex != i)
{
SolidBrush sb = new SolidBrush(Color.FromArgb((int)dTrasp, c.R, c.G, c.B));
e.Graphics.FillRectangle(sb, t.m_Rect);
e.Graphics.FillRectangle(sb, t.m_RectShadow);
}
if (nIndex == i)
e.Graphics.DrawRectangle(Pens.White, t.m_Rect);
}
for (int i=0;i
Tiếp theo là khai báo các đối tượng
class ThumbElement
{
static public int m_ThumbSize=200;
public Bitmap m_bmpOriginal = null;
public Bitmap m_bmpMain= null;
public Bitmap m_bmpShadow = null;
public double m_dAngleOriginal = 0;
public double m_dAngleActual = 0;
public double m_dDistanceFromScreen = 0;
public Rectangle m_Rect = new Rectangle();
public Rectangle m_RectShadow = new Rectangle();
public ThumbElement(String strFileName)
{
m_bmpOriginal = new Bitmap(strFileName);
int nWidth=m_ThumbSize;
int nHeight=m_bmpOriginal.Height*m_ThumbSize/m_bmpOriginal.Width;
if (nHeight>m_ThumbSize)
{
nHeight=m_ThumbSize;
nWidth=m_bmpOriginal.Width*m_ThumbSize/m_bmpOriginal.Height;
}
m_bmpMain=new Bitmap(nWidth,nHeight);
Graphics g=Graphics.FromImage(m_bmpMain);
g.DrawImage(m_bmpOriginal, 0, 0, nWidth, nHeight);
m_bmpShadow = new Bitmap(m_bmpMain);
unsafe
{
m_bmpShadow.RotateFlip(RotateFlipType.RotateNoneFlipY);
System.Drawing.Imaging.BitmapData bmd = m_bmpShadow.LockBits(new Rectangle(0, 0, m_bmpShadow.Width, m_bmpShadow.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
m_bmpShadow.PixelFormat);
int PixelSize = 4;
byte* row = (byte*)bmd.Scan0;
for (int y = 0; y < bmd.Height; y++)
{
byte trasp = (byte)(100 * ((m_bmpMain.Height - y)) / m_bmpMain.Height);
int xx = 3;
for (int x = 0; x < bmd.Width; x++)
{
row[xx] = trasp;
xx += PixelSize;
}
row += bmd.Stride;
}
m_bmpShadow.UnlockBits(bmd);
}
}
}
class FlyingObject
{
public Bitmap m_bmpFLyingBitmap = null;
public Rectangle m_rcFBStart = new Rectangle();
public Rectangle m_rcFB = new Rectangle();
public int m_nFBPerc = 0;
public int m_nFBState = 0;
public ThumbElement m_tRef = null;
}
Chúc vui vẻ!