You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用iTextSharp将Panel截图插入PDF后图片质量变差如何解决

问题根因
  • 图像编码格式选择问题:代码中使用默认参数的JPEG格式,压缩率高导致边缘细节丢失
  • 原始图像分辨率不足:DrawToBitmap 默认采集屏幕96DPI的视图,分辨率低,放入PDF缩放后就会出现模糊
  • 多次压缩叠加损失:先把Bitmap存为JPEG、再传给iTextSharp的操作造成两次压缩,画质损失叠加
  • iTextSharp默认压缩:iTextSharp默认会对插入的图像进行二次压缩,进一步降低画质
修复方案
  1. 提升原始渲染分辨率:将Panel视图渲染为2-3倍于原尺寸的高DPI位图,从源头提升图像清晰度
  2. 改用无损编码格式:使用PNG格式传递和保存图像,避免JPEG压缩带来的细节损失
  3. 关闭iTextSharp自动压缩:禁用PDF写入时的图像二次压缩,保留原始画质
  4. 优化渲染流程:使用Graphics缩放绘制高分辨率位图,保证渲染内容比例正确

修改后的完整代码如下:

iTextSharp.text.Document doc = new iTextSharp.text.Document();
// 禁用iText的压缩,避免二次画质损失
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(sfd.FileName, FileMode.Create));
writer.CompressionLevel = 0;

panelTombi.AutoScrollPosition = new Point(0, 0);
panelTombi.AutoSize = true;
panelTombi.Refresh();
int baseWidth = panelTombi.Width;
int baseHeight = panelTombi.Height;
// 渲染缩放系数,可根据清晰度需求调整为2-3倍
int renderScale = 2;
int width = baseWidth * renderScale;
int height = baseHeight * renderScale;

Bitmap MemoryImage = new Bitmap(width, height);
// 设置高DPI匹配缩放后的分辨率
MemoryImage.SetResolution(96 * renderScale, 96 * renderScale);
using (Graphics g = Graphics.FromImage(MemoryImage))
{
    g.ScaleTransform(renderScale, renderScale);
    g.Clear(panelTombi.BackColor);
    panelTombi.DrawToBitmap(MemoryImage, new System.Drawing.Rectangle(0, 0, baseWidth, baseHeight));
}

// 改用PNG无损格式传递图像
iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance((System.Drawing.Image)MemoryImage, System.Drawing.Imaging.ImageFormat.Png);
image1.CompressionLevel = 0;
                       
image1.ScaleToFit(doc.PageSize);   
image1.SetAbsolutePosition(0, PageSize.A4.Height - image1.ScaledHeight - doc.TopMargin);
// 本地保存也使用PNG格式,如需JPEG可单独指定100质量等级保存
MemoryImage.Save(Path.GetDirectoryName(sfd.FileName) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(sfd.FileName) + ".png", System.Drawing.Imaging.ImageFormat.Png); 
                        
doc.Open();
doc.Add(image1);
doc.Close();
MemoryImage.Dispose();

如果业务要求必须使用JPEG格式,可在图像转换、保存环节单独指定JPEG质量为100,最大程度降低压缩损失。

内容的提问来源于stack exchange,提问作者jejedu67

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 04:06:03