如何更改无法从 'System.Drawing.Image' 转换为 'System.Drawing.Image[]?
Posted
技术标签:
【中文标题】如何更改无法从 \'System.Drawing.Image\' 转换为 \'System.Drawing.Image[]?【英文标题】:How to change cannot convert from 'System.Drawing.Image' to 'System.Drawing.Image[]?如何更改无法从 'System.Drawing.Image' 转换为 'System.Drawing.Image[]? 【发布时间】:2022-01-24 01:27:47 【问题描述】:大家好,我正在尝试将 'System.Drawing.Image' 更改为 'System.Drawing.Image[] 以在我的代码中使用,因此如何使它工作,因为我需要合并 tiff 图像以存在 Multi Tif 图像谢谢。 如果我需要在选择页面后添加页面以存在多 tif 图像怎么办?如果还有其他方法可以帮助我吗?
Image img2 = Image.FromFile(imagename1);
Image img = Image.FromFile(imagename2);
Tiff.TiffUtil.saveImageExistingMultiplePage(img2, img, 2,
imagename2);
public static void saveImageExistingMultiplePage(Image[] bmp, Image origionalFile, int PageNumber, string location)
try
//Now load the Codecs
ImageCodecInfo codecInfo = getCodec();
System.Drawing.Imaging.Encoder saveEncoder;
System.Drawing.Imaging.Encoder compressionEncoder;
EncoderParameter saveEncodeParam;
EncoderParameter compressionEncodeParam;
EncoderParameters encoderParams = new EncoderParameters(2);
Bitmap pages;
Bitmap nextPage;
saveEncoder = System.Drawing.Imaging.Encoder.SaveFlag;
compressionEncoder = System.Drawing.Imaging.Encoder.Compression;
origionalFile.SelectActiveFrame(FrameDimension.Page, 0);
pages = new Bitmap(origionalFile);
pages = ConvertToBitonal(pages);
// Save the first page (frame).
saveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.MultiFrame);
compressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4);
encoderParams.Param[0] = compressionEncodeParam;
encoderParams.Param[1] = saveEncodeParam;
pages.Save(location, codecInfo, encoderParams);
for (int i = 1; i < PageNumber; i++)
saveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.FrameDimensionPage);
compressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4);
encoderParams.Param[0] = compressionEncodeParam;
encoderParams.Param[1] = saveEncodeParam;
origionalFile.SelectActiveFrame(FrameDimension.Page, i);
nextPage = new Bitmap(origionalFile);
nextPage = ConvertToBitonal(nextPage);
pages.SaveAdd(nextPage, encoderParams);
for (int i = 0; i < bmp.Length; i++)
saveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.FrameDimensionPage);
compressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4);
encoderParams.Param[0] = compressionEncodeParam;
encoderParams.Param[1] = saveEncodeParam;
bmp[i] = (Bitmap)ConvertToBitonal((Bitmap)bmp[i]);
pages.SaveAdd(bmp[i], encoderParams);
saveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.Flush);
encoderParams.Param[0] = saveEncodeParam;
pages.SaveAdd(encoderParams);
catch (System.Exception ee)
throw ee;
【问题讨论】:
【参考方案1】:// initialize an image array and add your image to it
Image[] img2 = new Image[] Image.FromFile(imagename1) ;
// the rest of your code stays the same but 'img2' is now an array of images
// so the call to 'saveImageExistingMultiplePage' will accept it.
// ...
Tiff.TiffUtil.saveImageExistingMultiplePage(img2, img, 2, imagename2);
// ... continue your code
【讨论】:
以上是关于如何更改无法从 'System.Drawing.Image' 转换为 'System.Drawing.Image[]?的主要内容,如果未能解决你的问题,请参考以下文章
无法从 IEnumerable<Model> 更改 foreach 中的值