惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Stack Overflow Blog
Stack Overflow Blog
人人都是产品经理
人人都是产品经理
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
博客园 - 司徒正美
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog

博客园 - Hotcan

Microsoft Azure Project Oxford 体验 使用CSDN Code将网站部署到Windows Azure Website上 各种云计算虚拟机大比拼 云计算里AWS和Azure的探究(6) - Amazon Simple Storage Service 和 Microsoft Azure Blob Storage 云计算里AWS和Azure的探究(5) ——EC2和Azure VM磁盘性能分析 Windows Azure Overview 云计算里AWS和Azure的探究(4) 云计算里AWS和Azure的探究(3) 云计算里AWS和Azure的探究(2.1) 云计算里AWS和Azure的探究(1) 云计算里AWS和Azure的探究(2) GDI+中常见的几个问题(11) GDI+中常见的几个问题(10) GDI+中常见的几个问题(9) GDI+中常见的几个问题(8) GDI+中常见的几个问题(8.外传1) GDI+中常见的几个问题(6) GDI+中常见的几个问题(5) GDI+中常见的几个问题(4)
GDI+中常见的几个问题(7)
Hotcan · 2008-10-31 · via 博客园 - Hotcan

7. 多帧图像

为了赶上英雄第三季的播放日程,我决定一个星期出一集。 在第七集Heroes里面,Peter的功能都被他老爸吸收掉了。所以我的这个系列的第七集来讲讲GDI+没完全实现的一部分功能。

多帧图像是指在一幅图像中有多个帧,支持多帧图像的格式不多,只有TIFF和GIF。其他格式都不能作为多帧图像存储。其中TIFF可以支持很多页,GIF动画也支持多帧。使用GDI+可以生成多帧TIFF,却没办法实现GIF动画的生成,有可能是因为专利的缘故。首先让我们来看看怎么样在生成多帧的TIFF图像。

 1         public void CreateMultiframeTIFF(string resultImage, string image1, params string [] images)
 2         {
 3             //Read multiple frames, the frames' size can be different
 4             Image frame1 = Image.FromFile(image1);
 5             int length = images.Length;
 6             Image[] frames = new Image[length];
 7             for (int i = 0; i < length; i++)
 8             {
 9                 frames[i] = Image.FromFile(images[i]);  
10             }
11             
12             //Set the first frame as the base bitmap
13             Bitmap bmpResult = (Bitmap)frame1;
14             
15             //Create encoder parameters with different values
16             EncoderParameters parameters = new EncoderParameters(1);
17             parameters.Param[0= new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
18 
19             //Find Tiff codec
20             List<ImageCodecInfo> supportedCodecs = new List<ImageCodecInfo> ();
21             supportedCodecs.AddRange(ImageCodecInfo.GetImageEncoders());
22             ImageCodecInfo tiffCodecInfo = supportedCodecs.Find(
23                 delegate(ImageCodecInfo info)
24                 {
25                     return info.MimeType.Equals(
26                         System.Net.Mime.MediaTypeNames.Image.Tiff,
27                         StringComparison.OrdinalIgnoreCase);
28                 }
29             );
30             
31             //Save the first frame to a stream.
32             bmpResult.Save(resultImage, tiffCodecInfo, parameters);
33 
34             //Save the second frame into the tiff
35             parameters.Param[0= new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
36             foreach (Image frame in frames)
37             {
38                 bmpResult.SaveAdd(frame, parameters);
39             }
40 
41             //flush the stream
42             parameters.Param[0= new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.Flush);
43             bmpResult.SaveAdd(parameters);
44 
45             //dispose all resources
46             frame1.Dispose();
47             foreach (Image frame in frames)
48             {
49                 frame.Dispose();
50             }
51             bmpResult.Dispose();         
52         }

这里我们使用了EncoderParameters来设置图像编码的参数,首先在第一帧保存的时候设置多帧属性(16,17行),然后拿到Tiff的编码器(20 - 29行)。先使用EncoderParamters 和 ImageCodecInfo对第一帧进行保存,然后使用SaveAdd方法添加新的帧。最后Flush了一下流,其实是可选的,因为在Bitmap dispose的时候会自动flush。

读取多帧TIFF比较简单,直接调用GetFrameCount和SelectActiveFrame就可以了,我们可以通过下面代码行1获得所有帧数,通过行2选择当前的帧。

1             int count = bmp.GetFrameCount(FrameDimension.Page);
2             bmp.SelectActiveFrame(FrameDimension.Page, 1);

GDI+可以读取GIF动画,所不同的是在上面代码1,2行中,要使用FrameDimension.Time参数传入。虽然GDI+没有现成的函数实现生成GIF动画,不过可以通过别的办法来实现。这年头没有做不到的,只有想不到的。不过那个方法过于复杂和底层了,等啥时候Heroes里面Peter拿回能力了,我再告诉大家。

今天就先到这里了。