+ UGif: GIF图片解析类

This commit is contained in:
Sunny 2021-07-17 00:15:29 +08:00
parent 929a86b4b6
commit 5fbfe7cd4d
10 changed files with 217 additions and 5 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -34,7 +34,7 @@ namespace Sunny.UI
private int XPos = int.MinValue; private int XPos = int.MinValue;
private int XPos1 = int.MaxValue; private int XPos1 = int.MaxValue;
private int interval = 200; private int interval = 200;
private int TextWidth = Int32.MinValue; private int TextWidth = int.MinValue;
public UIScrollingText() public UIScrollingText()
{ {
@ -104,7 +104,7 @@ namespace Sunny.UI
private void Timer_Tick(object sender, EventArgs e) private void Timer_Tick(object sender, EventArgs e)
{ {
if (XPos == Int32.MinValue) if (XPos == int.MinValue)
{ {
Invalidate(); Invalidate();
} }

View File

@ -42,7 +42,7 @@
this.PageTitle.Padding = new System.Windows.Forms.Padding(6, 0, 0, 0); this.PageTitle.Padding = new System.Windows.Forms.Padding(6, 0, 0, 0);
this.PageTitle.RadiusSides = Sunny.UI.UICornerRadiusSides.None; this.PageTitle.RadiusSides = Sunny.UI.UICornerRadiusSides.None;
this.PageTitle.RectSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.None; this.PageTitle.RectSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.None;
this.PageTitle.Size = new System.Drawing.Size(913, 35); this.PageTitle.Size = new System.Drawing.Size(1399, 35);
this.PageTitle.Symbol = 0; this.PageTitle.Symbol = 0;
this.PageTitle.SymbolSize = 24; this.PageTitle.SymbolSize = 24;
this.PageTitle.TabIndex = 0; this.PageTitle.TabIndex = 0;
@ -59,7 +59,7 @@
this.PagePanel.Name = "PagePanel"; this.PagePanel.Name = "PagePanel";
this.PagePanel.RadiusSides = Sunny.UI.UICornerRadiusSides.None; this.PagePanel.RadiusSides = Sunny.UI.UICornerRadiusSides.None;
this.PagePanel.RectSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.None; this.PagePanel.RectSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.None;
this.PagePanel.Size = new System.Drawing.Size(913, 367); this.PagePanel.Size = new System.Drawing.Size(1399, 878);
this.PagePanel.TabIndex = 1; this.PagePanel.TabIndex = 1;
this.PagePanel.Text = null; this.PagePanel.Text = null;
this.PagePanel.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter; this.PagePanel.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
@ -67,7 +67,7 @@
// UITitlePage // UITitlePage
// //
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(913, 402); this.ClientSize = new System.Drawing.Size(1399, 913);
this.Controls.Add(this.PagePanel); this.Controls.Add(this.PagePanel);
this.Controls.Add(this.PageTitle); this.Controls.Add(this.PageTitle);
this.Name = "UITitlePage"; this.Name = "UITitlePage";

View File

@ -31,6 +31,23 @@ namespace Sunny.UI
public UITitlePage() public UITitlePage()
{ {
InitializeComponent(); InitializeComponent();
base.BackColor = UIColor.LightBlue;
TopLevel = false;
if (this.Register()) SetStyle(UIStyles.Style);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
UpdateStyles();
if (!IsDesignMode) base.Dock = DockStyle.Fill;
Version = UIGlobal.Version;
}
public new string Version
{
get;
} }
private string text; private string text;

195
SunnyUI/Units/UGif.cs Normal file
View File

@ -0,0 +1,195 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace Sunny.UI
{
public class Gif : IDisposable
{
public Gif(string fileName)
{
timer = new Timer();
timer.Tick += Timer_Tick;
if (FileEx.Exists(fileName))
{
Image = Image.FromFile(fileName);
}
}
public Gif(Image img)
{
timer = new Timer();
timer.Tick += Timer_Tick;
if (img != null)
{
Image = img;
}
}
public bool Loop
{
get; set;
}
public void Reset()
{
ImageCount = 0;
FrameIndex = 0;
Active = false;
}
private void Timer_Tick(object sender, EventArgs e)
{
timer.Stop();
if (Image == null)
{
Reset();
return;
}
if (IsGif)
{
if (FrameIndex < ImageCount - 1)
{
FrameIndex++;
}
else
{
if (Loop)
{
FrameIndex = 0;
}
else
{
active = false;
}
}
FrameDimension fd = image.GifFrameDimension();
Image.SelectActiveFrame(fd, FrameIndex);
}
ImageChanged?.Invoke(this, Image);
timer.Enabled = IsGif && active;
}
public Color BackColor { get; set; } = Color.White;
public delegate void OnImageChanged(object sender, Image image);
public event OnImageChanged ImageChanged;
private readonly Timer timer;
private Image image;
private int ImageCount;
public bool IsGif => ImageCount > 0;
public Image Image
{
get => image;
set
{
Reset();
image = value;
if (Image != null)
{
ImageCount = image.GifFrameCount();
if (IsGif)
{
int delay = image.GifFrameInterval();
if (delay > 0)
{
timer.Interval = delay;
}
}
}
}
}
public void Invalidate()
{
if (!Active)
{
Active = true;
}
}
public void Dispose()
{
Reset();
Image?.Dispose();
Image = null;
}
private int FrameIndex;
private bool active;
public bool Active
{
get => active;
set
{
ImageCount = 0;
FrameIndex = 0;
active = value;
timer.Enabled = value;
}
}
}
public static class GifHelper
{
public static FrameDimension GifFrameDimension(this Image img)
{
return new FrameDimension(img.FrameDimensionsList[0]);
}
public static int GifFrameCount(this Image img)
{
if (img == null) return 0;
FrameDimension fd = new FrameDimension(img.FrameDimensionsList[0]);
return img.GetFrameCount(fd);
}
public static int GifFrameInterval(this Image img)
{
FrameDimension dim = new FrameDimension(img.FrameDimensionsList[0]);
int frameCount = img.GetFrameCount(dim);
if (frameCount <= 1)
return 0;
int delay = 0;
bool stop = false;
for (int i = 0; i < frameCount; i++)//遍历图像帧
{
if (stop) break;
img.SelectActiveFrame(dim, i);//激活当前帧
for (int j = 0; j < img.PropertyIdList.Length; j++)//遍历帧属性
{
if ((int)img.PropertyIdList.GetValue(j) == 0x5100)//如果是延迟时间
{
PropertyItem pItem = (PropertyItem)img.PropertyItems.GetValue(j);//获取延迟时间属性
byte[] delayByte = new byte[4];//延迟时间以1/100秒为单位
delayByte[0] = pItem.Value[i * 4];
delayByte[1] = pItem.Value[1 + i * 4];
delayByte[2] = pItem.Value[2 + i * 4];
delayByte[3] = pItem.Value[3 + i * 4];
delay = BitConverter.ToInt32(delayByte, 0) * 10; //乘以10获取到毫秒
stop = true;
break;
}
}
}
return delay;
}
}
}