我将计时器刻度事件设置为250毫秒:
private void timer4_Tick(object sender, EventArgs e)
{
if (pictureboximagestosavecount == 72)
{
timer4.Enabled = false;
}
else
{
Bitmap bmp = new Bitmap(this.Width, this.Height);
Rectangle rect = new Rectangle(this.Location.X, this.Location.Y, this.Width, this.Height);
pictureboximagestosavecount++;
savePictureBox(pictureBox1, @"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "pbimg.gif");
this.DrawToBitmap(bmp, rect);
bmp.Save(@"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "form.gif");
}
}
Run Code Online (Sandbox Code Playgroud)
首先,我使用savePictureBox方法将pictureBox保存为gif.其次我保存form1:
bmp.Save(@"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "form.gif");
Run Code Online (Sandbox Code Playgroud)
在timer4停止后,我有一个基本点击事件,我从保存的文件中创建动画gif:
private void animatedgifbutton_Click(object sender, EventArgs e)
{
DirectoryInfo di1;
FileInfo[] fi1;
di1 = new DirectoryInfo(@"c:\temp\pboximages\");
fi1 = di1.GetFiles("*form.gif");
List<string> newImages = new List<string>();
for (int i = 0; i < fi1.Length; i++)
{
newImages.Add(fi1[i].FullName);
}
animatedgif.MakeGIF(newImages, @"c:\temp\pboximages\animated1.gif", 6, true);
}
Run Code Online (Sandbox Code Playgroud)
当我正在做*.form.gif时,我在List newImages中只看到gifs格式的文件animatedgif.MakeGIF抛出错误,因为它需要获取GIF文件列表,但我想当我保存表单时它将它保存为位图而不是真正的gif.
如何获取form1的屏幕截图并将其保存为真正的gif?当我将pictureBox1保存到硬盘时,它是GIF,没有问题.
问题在于保存表单.
编辑:
我现在也看到我保存form1的截图的方式并不好,它不是只保存整个表单区域的一部分.我猜这个使用这个bmp和rect并不好.这是我在保存表单截图时的示例:

换句话说我需要在timer4 tick事件中将表单屏幕整个表单保存到硬盘上作为gif文件真正的gif文件而不是bmp.
它没有保存为正确,GIF因为你没有告诉它.扩展是不够的,你需要添加ImageFormat到Save通话!
并且它不会保存整个区域,Form因为您没有使用权限Rectangle来控制它.
如果您使用修改后的savePictueBox函数,这两个问题都会很简单:
void saveControl(Control Ctl, string fileName)
{
Rectangle Rect = Ctl.ClientRectangle;
// if (Ctl is Form) Rect = Ctl.Bounds; // (*)
using (Bitmap bmp = new Bitmap(Rect.Width, Rect.Height))
{
Ctl.DrawToBitmap(bmp, Rect );
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这使用了ClientRectangle,即它省略了Form边框.如果您想要包含边框,您可以取消注释该行(*)
现在你可以称之为
saveControl(pictureBox1, yourFileName);
Run Code Online (Sandbox Code Playgroud)
并作为
saveControl(this, yourOtherFileName);
Run Code Online (Sandbox Code Playgroud)
关于GIF动画的两个注释:
| 归档时间: |
|
| 查看次数: |
192 次 |
| 最近记录: |