简体   繁体   English

如何从磁盘平滑图像的边缘/导出到pdf(c#)

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

im using itextsharp for exporting the image to pdf. 即时通讯使用itextsharp将图像导出为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 我想使图像的边缘平滑(弯曲的边缘),并且通过itextsharp image属性来获取图像的宽度和高度(同时从磁盘获取图像),以及如何设置pdf页面的背景色

following is for getting image and adding to pdf: 以下是获取图像并添加到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? 问题1:图片的大小是多少?

You have an Image instance gif . 您有一个Image实例gif The witdh of this image is gif.ScaledWidth and jpg.ScaledHeight . 该图像的gif.ScaledWidthgif.ScaledWidthjpg.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. 还有其他获取宽度和高度的方法,但是这种方法始终为您提供将在PDF中使用的用户单位的大小。

If you do not scale the image, ScaledWidth and ScaledHeight will give you the original size of the image in pixels. 如果不缩放图像, ScaledWidthScaledHeight将为您提供图像的原始大小(以像素为单位)。 Pixels will be treated as user units by iText. 像素将被iText视为用户单位。 In PDF, a user unit corresponds with a point by default (and 72 points correspond with 1 inch). 在PDF中,默认情况下,用户单位与一个点相对应(72个点与1英寸相对应)。

Question 2: How do you display the image with rounded corners? 问题2:如何显示带有圆角的图像?

Some image formats (such as PNG) allow transparency. 某些图像格式(例如PNG)允许透明。 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. 在本书第10章的ClippingPath示例中对此进行了演示。

Ported to C#, the example would be something like this: 移植到C#后,示例将如下所示:

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 . 当然:如生成的PDF所示,这会将图像剪切为椭圆形。 You need to replace the Ellipse() method from the example with the RoundRectangle() method. 您需要将示例中的Ellipse()方法替换为RoundRectangle()方法。

Question3: How to give each page a background color? 问题3:如何为每页赋予背景色?

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. 使用页面事件添加背景色,您将在上述问题中找到有关如何执行此操作的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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