在我的服务器的 public/images
文件夹中,我上传了 2048x1536 像素和 290 kb 的 jpg 图像
。
我已经在MySQL表数据库中的字段Pic
上记住了这个链接:http://mywebserver.xxx/aspx/public/images/IMG0006A.jpg
我需要使用 iTextSharp library 将此图像打印在 pdf 文件上并调整了该图像的大小,因为其原始文件大小超出了 pdf 页码。
我发现这个类似question我尝试了该建议,但没有成功,因为在输出中,打印在 pdf 文件上的 jpg 图像始终位于页面之外。
如何解决这个问题?
你能帮我吗?
预先感谢您,我的代码如下。
.aspx 标记
<table border="1">
<tr>
<td colspan="2">
<label for="Pic">
Pic<br />
</label>
<asp:Image ID="Pic" runat="server" /></td>
</tr>
</table>
.cs 代码隐藏
if (!String.IsNullOrEmpty(reader["Pic"].ToString()))
{
lbPic.ImageUrl = reader["Pic"].ToString();
iTextSharp.text.pdf.PdfPCell imgCell1 = new iTextSharp.text.pdf.PdfPCell();
iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(Server.MapPath("\\public\\images\\IMG0006A.jpg"));
imgCell1.AddElement(new Chunk(img1, 0, 0));
img1.ScaleToFit(120f, 155.25f);
img1.ScaleAbsolute(120f, 155.25f);
}
最佳答案
我不使用也不了解iTextSharp库,但我认为您可以使用上传文件中的调整图像大小方法解决此问题>.
例如
private int ResizeMethod(FileUpload upload, int iFileCnt)
{
Stream strm = upload.PostedFile.InputStream;
using (var image = System.Drawing.Image.FromStream(strm))
{
int newWidth = 450;
int newHeight = 350;
var thumbImg = new Bitmap(newWidth, newHeight);
var thumbGraph = Graphics.FromImage(thumbImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imgRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbGraph.DrawImage(image, imgRectangle);
string extension = Path.GetExtension(upload.PostedFile.FileName);
string targetPath = Server.MapPath("\\public\\images\\" + upload.FileName.ToString());
thumbImg.Save(targetPath, image.RawFormat);
iFileCnt += 1;
}
return iFileCnt;
}
希望这对您有帮助-
关于c# - 在 C# 中使用 iTextSharp 和 asp 图像调整数据库中的图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38869798/