简体   繁体   中英

how to smooth the edge of the image from disk/exporting to pdf(c#)

im using itextsharp for exporting the image to pdf. ---- i want to make the edges of image smooth (curved edges), ---- and by itextsharp image property to get the image width and height(while getting the image from disk) ---- and also how to set the background color of the pdf page

following is for getting image and adding to pdf:

 pdfDoc.Open();
        //pdfDoc.Add(new iTextSharp.text.Paragraph("Welcome to dotnetfox"));
        iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(@"C:\Users\Admin\Desktop\logoall.bmp");
       // gif.ScaleToFit(500, 100);
        pdfDoc.Add(gif);

following is making grid to image and saving to disk:

Grid companysnapshot = values[0] as Grid;    //companysnap shot

        companysnapshot.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
        int companywidth = (int)Math.Round(companysnapshot.ActualWidth);
        int companyheight = (int)Math.Round(companysnapshot.ActualHeight);
        companywidth = companywidth == 0 ? 1 : companywidth;
        companyheight = companyheight == 0 ? 1 : companyheight;

        RenderTargetBitmap rtbmp = new RenderTargetBitmap(companywidth, companyheight, 96d, 96d, PixelFormats.Default);
        rtbmp.Render(companysnapshot);
        BmpBitmapEncoder encoder = new BmpBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(rtbmp));
        FileStream fs1 = File.Create(@"C:\Users\Admin\Desktop\companyss.bmp");
        encoder.Save(fs1);
        fs1.Close();

please code me out for this!!

You have combined multiple question into one post. That's not the way you should post questions.

Anyway:

Question 1: What is the size of an image?

You have an Image instance gif . The witdh of this image is gif.ScaledWidth and jpg.ScaledHeight . There are other ways to get the width and the height, but this way always gives you the size in user units that will be used in the PDF.

If you do not scale the image, ScaledWidth and ScaledHeight will give you the original size of the image in pixels. Pixels will be treated as user units by iText. In PDF, a user unit corresponds with a point by default (and 72 points correspond with 1 inch).

Question 2: How do you display the image with rounded corners?

Some image formats (such as PNG) allow transparency. You could create an image in such a way that the effect of rounded corners is mimicked by making the corners transparent.

If this is not an option, you should apply a clipping path. This is demonstrated in the ClippingPath example in chapter 10 of my book.

Ported to C#, the example would be something like this:

Image img = Image.GetInstance(some_path_to_an_image);
float w = img.ScaledWidth;
float h = img.ScaledHeight;
PdfTemplate t = writer.DirectContent.CreateTemplate(w, h);
t.Ellipse(0, 0, w, h);
t.Clip();
t.NewPath();
t.AddImage(img, w, 0, 0, h, 0, -600);
Image clipped = Image.GetInstance(t);

Of course: this clips the image into an ellipse as shown in the resulting PDF . You need to replace the Ellipse() method from the example with the RoundRectangle() method.

Question3: How to give each page a background color?

This is a duplicate question. Please read the answers to the following questions:

Adding a background color is done using page events and you'll find the code on how to do this in the questions mentioned above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM