2014-08-28 18 views
1

我有一个多页图像abc.tiff,我必须在每个页面上做一些绘图并将其保存为一个D:\xyz位置的多页图像。如何从图像列表制作多页图像

我使用下面的代码为:

List<Image> images = new List<Image>(); 
    Bitmap bitmap = (Bitmap)Image.FromFile(@"abc.tiff"); 
    int count = bitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page); 

    for (int idx = 0; idx < count ; idx++) 
    { 

     bitmap.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, idx); 
     // save each frame to a bytestream 
     MemoryStream byteStream = new MemoryStream(); 
     // below 3 lines for drawing something on image... 
     Bitmap tmp = new Bitmap(bitmap, bitmap.Width, bitmap.Height); 
     Graphics g = Graphics.FromImage(tmp); 
     g.DrawRectangle(blackPen, x, y, width, height); 

     tmp.Save(byteStream, System.Drawing.Imaging.ImageFormat.Tiff); 
     tmp.Dispose(); 
     // and finally adding each frame into image list  
     images.Add(Image.FromStream(byteStream)); 
    } 

之后,我想,以节省D:\xyz位置我改良的多页图像。

请问我可以如何从List<Image>图片获得一个多页图片?

+0

你解决了你的问题吗? – TaW 2014-09-09 13:55:15

回答

1

这是采取非常直接从Bob Powell

假设

string saveName = "c:\\myMultiPage.tiff" // your target path 
List<Image> imgList = new List<Image>();  // your list of images 

当列表填充,你可以这样做:

using System.Drawing.Imaging; 
// .. 

System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag; 

// create one master bitmap from the first image 
Bitmap master = new Bitmap(imgList[0]); 
ImageCodecInfo info = null; 

// lets hope we find a tiff encoder! 
foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders()) 
    if (ice.MimeType == "image/tiff") info = ice; 

// we'll always need only one parameter 
EncoderParameters ep = new EncoderParameters(1); 
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame); 

// save the master with our parameter, announcing it will be 'MultiFrame' 
master.Save(saveName, info, ep); 

// now change the parameter to 'FramePage'.. 
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); 

// ..and add-save the other images into the master file 
for (int i = 1; i < imgList.Count; i++) 
    master.SaveAdd(imgList[i], ep); 

// finally set the parameter to 'Flush' and do it.. 
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush); 
master.SaveAdd(ep); 

所有赞美鲍勃·鲍威尔!