将位图转换为.NET 2.0中的一个多页TIFF图像
如何将位图数组转换为TIFF格式的全新图像,在此新的tiff图像中添加所有位图作为帧?
使用.NET 2.0。
将第一个位图放入一个Image对象开始
Bitmap bitmap = (Bitmap)Image.FromFile(file);
将位图保存为tiff
MemoryStream byteStream = new MemoryStream(); bitmap.Save(byteStream, ImageFormat.Tiff);
将Tiff放入另一个Image对象
Image tiff = Image.FromStream(byteStream)
准备编码器:
ImageCodecInfo encoderInfo = GetEncoderInfo("image/tiff"); EncoderParameters encoderParams = new EncoderParameters(2); EncoderParameter parameter = new EncoderParameter( Encoder.Compression, (long)EncoderValue.CompressionCCITT4); encoderParams.Param[0] = parameter; parameter = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame); encoderParams.Param[1] = parameter;
保存到文件:
tiff.Save(sOutFilePath, encoderInfo, encoderParams);
对于后续页面,准备编码器:
EncoderParameters EncoderParams = new EncoderParameters(2); EncoderParameter SaveEncodeParam = new EncoderParameter( Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); EncoderParameter CompressionEncodeParam = new EncoderParameter( Encoder.Compression, (long)EncoderValue.CompressionCCITT4); EncoderParams.Param[0] = CompressionEncodeParam; EncoderParams.Param[1] = SaveEncodeParam; tiff.SaveAdd(/* next image as tiff - do the same as above with memory */, EncoderParams);
最后刷新文件:
EncoderParameter SaveEncodeParam = new EncoderParameter( Encoder.SaveFlag, (long)EncoderValue.Flush); EncoderParams = new EncoderParameters(1); EncoderParams.Param[0] = SaveEncodeParam; tiff.SaveAdd(EncoderParams);
这应该让你开始。
在Google上search一下之后,发现了这篇文章。 我试了一下a'b'c'd'e'f'g'h'的post里的代码,但是这对我没用。 也许,我没有做正确的事情。
在任何情况下,我发现另一篇文章,保存图像多页TIFF。 以下是该post的链接: http : //www.bobpowell.net/addframes.htm
另外,这里是为我工作的代码。 它应该是相同的职位。
Encoder encoder = Encoder.SaveFlag; ImageCodecInfo encoderInfo = ImageCodecInfo.GetImageEncoders().First(i => i.MimeType == "image/tiff"; EncoderParameters encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.MultiFrame); // Save the first frame of the multi page tiff Bitmap firstImage = (Bitmap) _scannedPages[0].RawContent; firstImage.Save(fileName, encoderInfo, encoderParameters); encoderParameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.FrameDimensionPage); // Add the remining images to the tiff for (int i = 1; i < _scannedPages.Count; i++) { Bitmap img = (Bitmap) _scannedPages[i].RawContent; firstImage.SaveAdd(img, encoderParameters); } // Close out the file encoderParameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.Flush); firstImage.SaveAdd(encoderParameters);
有用的话题。 谢谢(你的)信息。 我需要从base64编码的string数组缝合多页图像。 这是我基于这个线程中的信息放在一起。 我不太了解为什么我必须创build一个显式指定的图像格式的内存stream,但这是结束了工作,如果有更好的方式来处理这个请让我知道。 谢谢
/// <summary> /// Takes in an array of base64 encoded strings and creates a multipage tiff. /// </summary> /// <param name="sOutFile">file to be generated</param> /// <param name="pagesbase64Array"></param> private void SaevAsMultiPageTiff(string sOutFile, string[] pagesbase64Array) { System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.SaveFlag; ImageCodecInfo encoderInfo = ImageCodecInfo.GetImageEncoders().First(i => i.MimeType == "image/tiff"); EncoderParameters encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.MultiFrame); Bitmap firstImage = null; try { using (MemoryStream ms1 = new MemoryStream()) { using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(pagesbase64Array[0]))) { Image.FromStream(ms).Save(ms1, ImageFormat.Tiff); firstImage = (Bitmap)Image.FromStream(ms1); } // Save the first frame of the multi page tiff firstImage.Save(sOutFile, encoderInfo, encoderParameters); //throws Generic GDI+ error if the memory streams are not open when this is called } encoderParameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.FrameDimensionPage); Bitmap imagePage; // Add the remining images to the tiff for (int i = 1; i < pagesbase64Array.Length; i++) { using (MemoryStream ms1 = new MemoryStream()) { using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(pagesbase64Array[i]))) { Image.FromStream(ms).Save(ms1, ImageFormat.Tiff); imagePage = (Bitmap)Image.FromStream(ms1); } firstImage.SaveAdd(imagePage, encoderParameters); //throws Generic GDI+ error if the memory streams are not open when this is called } } } catch (Exception) { //ensure the errors are not missed while allowing for flush in finally block so files dont get locked up. throw; } finally { // Close out the file encoderParameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.Flush); firstImage.SaveAdd(encoderParameters); } }
在处理和创build标准化格式的文件时,不是微软的logging迷,我build议使用ImageMagick,以MagickNet的forms提供.Net库(请注意, http : //midimick.com/magicknet/目前有一个间谍软件popup窗口,我已经提醒网站所有者)。
这里是多tiff文件的拆分操作。 它就像string子串函数一样工作。 图像的第一个索引将是您的MasterBitMap,您将继续向MasterBitmap添加框架直到索引结束。
public void SaveMultiFrameTiff(int start, int end) { string outputFileName = "out.TIF"; string inputFileName = "input.TIF"; try { Bitmap MasterBitmap = new Bitmap(inputFileName ); //Start page of document(master) Image imageAdd = Image.FromFile(inputFileName ); //Frame Image that will be added to the master Guid guid = imageAdd.FrameDimensionsList[0]; //GUID FrameDimension dimension = new FrameDimension(guid); // start index cannot be less than 0 and cannot be greater than frame count if (start < 1 || end > MasterBitmap.GetFrameCount(dimension)) { return; } EncoderParameters ep = new EncoderParameters(1); //Get Image Codec Information ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo codecInfo = codecs[3]; //image/tiff //MultiFrame Encoding format EncoderParameter epMultiFrame = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); ep.Param[0] = epMultiFrame; MasterBitmap.SelectActiveFrame(dimension, start - 1); MasterBitmap.Save(outputFileName, codecInfo, ep);//create master document //FrameDimensionPage Encoding format EncoderParameter epFrameDimensionPage = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); ep.Param[0] = epFrameDimensionPage; for (int i = start; i < end; i++) { imageAdd.SelectActiveFrame(dimension, i);//select next frame MasterBitmap.SaveAdd(new Bitmap(imageAdd), ep);//add it to the master } //Flush Encoding format EncoderParameter epFlush = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush); ep.Param[0] = epFlush; MasterBitmap.SaveAdd(ep); //flush the file } catch (Exception ex) { throw ex; } } }