c# - How to create an image from a pdf rectangle

随声附和 提交于 2020-01-17 03:36:07

问题


is there a way to create an image based from pdf using rectangle?

im using syncfusion pdfviewer(using the Unlimited Flat-Fee License). and this is how i create an image from pdf using c#

private void ScreenCapture(string fileName, int x, int y, int width, int height)
        {
            try
            {

                if (x != 0 && y != 0 && width != 0 && height != 0)
                {
                    Rectangle rect = new Rectangle(x, y, width, height);
                    Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
                    Graphics g = Graphics.FromImage(bmp);
                    g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
                    bmp.Save(fileName, ImageFormat.Jpeg);
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.StackTrace);
            }
            finally
            {

            }
        }

and this is my additional code

on declaration

Point startPoint;

on mouse down

Control control = (Control)sender;

            startPoint = control.PointToScreen(new Point(e.X, e.Y));

on my mouse up

Point endPoint = ((Control)sender).PointToScreen(new Point(e.X, e.Y));
            int width = endPoint.X - startPoint.X;
            int height = endPoint.Y - startPoint.Y;
            theRectangle = new Rectangle(startPoint.X ,
                startPoint.Y, width, height );

the problem with my code is im capturing my screen. so when i resized my program. it will capture the screen and will result to wrong image

so is there a way to get or convert the pdf page that has a rectangle into image using c#? thank you

update:

im sorry if my question is not clear.

example. i draw a rectangle to the pdf using pdfviewer of syncfusion

the output will be something like this

https://drive.google.com/open?id=0B45rDxvaXzsmcTZIVVVSUU9Zc0E
https://drive.google.com/open?id=0B45rDxvaXzsmc1cxNTV4UUdOMUE
https://drive.google.com/open?id=0B45rDxvaXzsmSWtDRWhXYkpDT2c
https://drive.google.com/open?id=0B45rDxvaXzsmS214WmJnN3BvcUk

im verry sorry if my question is not clear


回答1:


After analyzed the attached screenshot, we found that you are using PdfViewerControl in WPF platform. As per your requirement, we have created a sample to convert the pdf page that has a rectangle to image using PDF Viewer.  

Steps for using the sample: 

  1. Draw the rectangle in an area using Shape Annotation as you have used in your screenshot. 

  2. Save the changes in PDF document using “SAVE” button of PdfViewerControl

  3. Click the button “Open PDF and ExportAsImage” for exporting the page that has a rectangle to image.

Please find the code snippet used convert the PDF page that has a rectangle into image:

PdfLoadedDocument ldoc = new PdfLoadedDocument(fileName); 

ldoc.Pages[0].Annotations.Flatten = true;
 

ldoc.Save(); 

Bitmap bmp = ldoc.ExportAsImage(0);
 

bmp.Save("outputImage.jpeg");

You can download the sample from the below link. 

Sample link: http://www.syncfusion.com/downloads/support/directtrac/166006/ze/SampleWPF734738560   

We have also create a video demo for using the sample and it will be available in the below link. 

Video link: http://www.syncfusion.com/downloads/support/directtrac/166006/ze/ExportAsImage1083458687 

Please try this sample and let us know whether it meets your requirement.

Otherwise kindly provide the specific details to us such as screenshot of your required output, sample you have used and other details (if any). It will be helpful for us to analyze more and to provide you better resolution.



来源:https://stackoverflow.com/questions/40126232/c-sharp-how-to-create-an-image-from-a-pdf-rectangle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!