【问题标题】:Resize image from database with iTextSharp and asp image in c#在 c# 中使用 iTextSharp 和 asp 图像调整数据库中的图像大小
【发布时间】:2016-08-10 09:46:15
【问题描述】:

在我服务器的public/images 文件夹中,我上传了jpg image,2048x1536 像素和290 kb。

我已经在 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);
    }

【问题讨论】:

  • 我想知道图像是如何显示的,因为您的单元格和图像对象都超出了该 sn-p 末尾的范围而没有其他用途..
  • 我不确定您是否需要同时使用 ScaleAbsoluteScaleToFit

标签: c# mysql asp.net pdf itext


【解决方案1】:

我不使用也不知道 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;
}

希望对你有所帮助-

【讨论】:

    猜你喜欢
    • 2011-03-02
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多