- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
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ẻ!