如何从图像列表中制作多页图像

时间:2014-08-28 06:28:51

标签: c# bitmapimage

我有一张多页图片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>图像中获取一张多页图像?

1 个答案:

答案 0 :(得分: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);

赞扬鲍勃鲍威尔!!