Antonio Mailtraq Antonio Mailtraq - 1 year ago 137
ASP.NET (C#) Question

Resize image from database with iTextSharp and asp image in c#

On the

folder in my server I have uploaded the
jpg image
with 2048x1536 pixel and 290 kb.

I have memorized in MySQL table database on the field
this link :

I need print this image on pdf file using iTextSharp library and resized this image because its original file size are off page pdf.

I have find this similar question and I have tried the suggestion without success because in the output the jpg image printed on pdf file is always out page.

How to do resolve this ?

Can you help me?

Thank you in advance, my code below.

.aspx markup

<table border="1">
<td colspan="2">
<label for="Pic">
Pic<br />
<asp:Image ID="Pic" runat="server" /></td>

.cs code-behind

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);

Answer Source

I don't use and don't know iTextSharp library but I think that you resolve this problem using resizing image method in upload file.


private int ResizeMetodo(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;

Hope that this is help for you-

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download