我一直在尝试调整从一组其他tiff创建的新tiff的分辨率。我想将分辨率降至100x100 dpi。连接的所有内容都能正常工作,但最终tiff中的分辨率将始终是我加入的文件的分辨率(使用300x300 dpi图像)。我尝试使用一些建议(Set DPI value to Tiff Image in C#)来获取/设置PropertyItems,但也失败了。使用下面的连接技术,将最终图像的分辨率设置为100x100 dpi的正确方法是什么?
谢谢。
public byte[] JoinTiffImages(
List<byte[]> images)
{
var fPage = FrameDimension.Page;
var nearest =
System.Drawing
.Drawing2D
.InterpolationMode
.NearestNeighbor;
if (images.Count == 0)
{
throw new ImageConverterException(
"Could not join an empty set of images.");
}
else if (images.Count == 1)
{
return images[0];
}
else
{
using (var ms = new MemoryStream())
{
using (var masterBitmap =
(Bitmap)DrawingImage.FromStream(
new MemoryStream(images[0])))
{
//get the codec for tiff files
var info = GetTifCodecInfo();
var enc = Encoder.SaveFlag;
var ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(
enc,
(long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(
Encoder.Compression,
(long)TifCompression.ToEncoderValue());
masterBitmap.SetResolution(100.0f, 100.0f);
masterBitmap.Save(ms, info, ep);
//save the intermediate frames
ep.Param[0] = new EncoderParameter(
enc,
(long)EncoderValue.FrameDimensionPage);
for (int i = 1; i < images.Count; i++)
{
using (var nextImg = (Bitmap)DrawingImage.FromStream(
new MemoryStream(images[i])))
{
for (int x = 0;
x < nextImg.GetFrameCount(fPage);
x++)
{
nextImg.SetResolution(100.0f, 100.0f);
nextImg.SelectActiveFrame(fPage, x);
masterBitmap.SaveAdd(nextImg, ep);
}
}
}
ep.Param[0] =
new EncoderParameter(
enc,
(long)EncoderValue.Flush);
//close out the file.
masterBitmap.SaveAdd(ep);
return ms.ToArray();
}
}
}
}
答案 0 :(得分:1)
这是一个从图像列表中创建多页tiff文件的代码。 它根据输入参数将DPI设置为主设备和所有部件。
private void saveTiff_Click(List<Image> imgList, string saveName, int dpi)
{
//all kudos to : http://bobpowell.net/generating_multipage_tiffs.aspx
foreach (Image img in imgList) ((Bitmap)img).SetResolution(dpi, dpi);
System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;
Bitmap master = new Bitmap(imgList[0]);
master.SetResolution(dpi, dpi);
ImageCodecInfo info = null;
// lets hope we have an TIF encoder
foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
if (ice.MimeType == "image/tiff") info = ice;
// one parameter: MultiFrame
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
master.Save(saveName, info, ep);
// one parameter: further frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
for (int i = 1; i < imgList.Count; i++) master.SaveAdd(imgList[i], ep);
// one parameter: flush
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
master.SaveAdd(ep);
}
答案 1 :(得分:1)
我能够在评论的帮助下解决我的问题。此功能将处理分辨率更改,而不会改变图像大小。
public static byte[] JoinTiffImages(
List<byte[]> images,
float res)
{
var fPage = FrameDimension.Page;
var nearest =
System.Drawing
.Drawing2D
.InterpolationMode
.NearestNeighbor;
if (images.Count == 0)
{
throw new Exception(
"Could not join an empty set of images.");
}
else if (images.Count == 1)
{
return images[0];
}
else
{
var ms = new MemoryStream();
//get the codec for tiff files
var info = GetTifCodecInfo();
var ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.SaveFlag,
(long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Compression,
(long)EncoderValue.CompressionLZW);
using (var masterImg =
(Bitmap)System.Drawing.Image.FromStream(
new MemoryStream(images[0])))
{
using (var resizedMaster =
new Bitmap(
(int)(masterImg.Width *
(res /
masterImg.HorizontalResolution)),
(int)(masterImg.Height *
(res /
masterImg.VerticalResolution))))
{
resizedMaster.SetResolution(
res,
res);
using (var gr = Graphics.FromImage(resizedMaster))
{
gr.InterpolationMode = nearest;
gr.DrawImage(
masterImg,
new Rectangle(
0,
0,
resizedMaster.Width,
resizedMaster.Height),
0,
0,
masterImg.Width,
masterImg.Height,
GraphicsUnit.Pixel);
}
resizedMaster.Save(ms, info, ep);
//save the intermediate frames
ep.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.SaveFlag,
(long)EncoderValue.FrameDimensionPage);
for (int i = 1; i < images.Count; i++)
{
using (var nextImg =
(Bitmap)System.Drawing.Image.FromStream(
new MemoryStream(images[i])))
{
for (int x = 0;
x < nextImg.GetFrameCount(fPage);
x++)
{
nextImg.SelectActiveFrame(fPage, x);
using (var resizedNext =
new Bitmap(
(int)(nextImg.Width *
(res /
nextImg.HorizontalResolution)),
(int)(nextImg.Height *
(res /
nextImg.VerticalResolution))))
{
resizedNext.SetResolution(
res,
res);
using (var gr =
Graphics.FromImage(resizedNext))
{
gr.InterpolationMode = nearest;
gr.DrawImage(
nextImg,
new Rectangle(
0,
0,
resizedNext.Width,
resizedNext.Height),
0,
0,
nextImg.Width,
nextImg.Height,
GraphicsUnit.Pixel);
}
resizedMaster.SaveAdd(resizedNext, ep);
}
}
}
}
ep.Param[0] =
new EncoderParameter(
System.Drawing.Imaging.Encoder.SaveFlag,
(long)EncoderValue.Flush);
//close out the file.
resizedMaster.SaveAdd(ep);
}
return ms.ToArray();
}
}
}
private static ImageCodecInfo GetTifCodecInfo()
{
foreach (var ice in ImageCodecInfo.GetImageEncoders())
{
if (ice.MimeType == "image/tiff")
{
return ice;
}
}
return null;
}