c# - 在 C# 中使用 iTextSharp 和 asp 图像调整数据库中的图像大小

标签 c# mysql asp.net pdf itext

在我的服务器的 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/

相关文章:

c# - 如何在文本框为空时输入默认值

mysql - SQL Server 到 Mysql 的迁移(使用 Mysql Workbench)。如何在工作台向导的步骤中添加带有手动转义撇号的数据?

ASP.NET HTTP 处理程序和全局变量

c# - 对选择合适的 ORM 感到困惑?

c# - 动态更改 C# 中枚举的值?

C# 套接字 TCP 客户端/服务器

c# - Autofac 作为 AWS Lambda Serverless ASP.NET Core 3.1 Web API 中的 IoC 容器

mysql - 同一查询中 count() 的平均值

javascript - 在javascript数组中显示mysql结果

javascript - IE8显示图像,Chrome/Firefox使用BlockUI不显示图像