在c#中使用iTextSharp和asp图像从数据库中调整图像大小
本文关键字:图像 数据库 调整 iTextSharp asp | 更新日期: 2023-09-27 18:01:34
在我的服务器的public/images
文件夹上,我上传了2048x1536像素和290 kb的jpg image
。
我在MySQL表数据库上背了字段Pic
这个链接:http://mywebserver.xxx/aspx/public/images/IMG0006A.jpg
我需要使用iTextSharp库在pdf文件上打印此图像并调整此图像的大小,因为其原始文件大小偏离pdf页面。
我发现了这个类似的问题,我尝试了这个建议,但没有成功,因为在输出中,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;
}
希望这对你有帮助-