Creating image renditions using docPrint - c#

I am trying to create multiple images of different resolution using DocPrint, in which I am facing problem with paper size of image. Below is the Image having resolution of 642x480 created using Docprit, if you right click the image and click on View Image/Open image in new tab you will see there are two white lines at the right and bottom of image .
the parameters which I am passing to DocPrint are:
string strAllOptions = "-* \"VL6IEN2GRW8EYN8P1D7F\"";
strAllOptions +=" -e -w 642 -h 480 -f 6.69x5 in";
docPrintObj.docPrintCOM_DocumentConverterEx(UserName, Password, SourcefilePath, OutputFilePath, strAllOptions);
where w = width in pixels, h = height pixels and f = paper size in inches
formula to get paper size (pixels * 0.01041667)
I tried giving different width and height while maintaining the Aspect ratio of image but was having the same problem every time.
Any type of help would be appreciated.
Note: I have to use DocPrint for creating image renditions.

Related

WPF Image control decimates large bitmaps

I'm writing an image viewer application that loads large still images. I must be able to zoom in to 1:1 to measure exact pixel coordinates of features in the image. I'm using the Viewport control posted here that works great for zoom and panning. I can load a 150 MP tiff image with BitmapDecoder and its pixel count is correct 14000 x 10000. However, when I assign this bitmap to the Image.Source property it gets decimated to roughly 15MP:
sourceBitmap.PixelWidth = 14204
sourceBitmap.PixelHeight = 10652
After assigning this bitmap to image1.Source we get
image1.Source.Width = 4545.27...
image1.Source.Height = 3408.64...
I'm aware of the unitless context of WPF graphics, and can work back the scale factors to read original coordinates, but there is a risk of rounding errors, and I'm working on a scaled copy that degrades the original image resolution.
According to the Microsoft documentation, a WPF bitmap can be up to 64GB in size, but the Image control seems not to be designed to work with bitmaps larger than 15MP. Setting Stretch to "None" makes things worst. It trims the image to the top left 4545 x 3408 pixels of the source and it displays it very small, almost as a thumbnail instead of 1:1.
Is there any way around this limitation?
In contrast to a BitmapSource's PixelWidth and PixelHeight, the Widthand Height values depend on its DPI (dots per inch), which is a TIFF or EXIF tag in the image file.
The values are identical when the resolution is 96 DPI, otherwise calculated as
Width = PixelWidth * 96 / DpiX
Height = PixelHeight * 96 / DpiY
Apparently, your images are tagged with 300 DPI.
This does in no way affect the pixel count, but just determines the native, unstretched size of the bitmap when it is shown in an Image element or ImageBrush.
Instead of using Width and Height, just keep using PixelWidth and PixelHeight:
var bitmap = (BitmapSource)image1.Source;
var width = bitmap.PixelWidth;
var height = bitmap.PixelHeight;

Getting "image" coordinates of picturebox in zoom mode

I am using a zoomed picturebox. I want to retrieve the image Top-Left and Bottom-Right coordinates. But it is different from that of picturebox when the aspect ratio of the image doesn't match the picturebox. I wonder how I can get the image coordinates on the form.
Minus the Image size divided by 2 from the PictureBoxsize plus the Image size again.
This uses the Size.Subtract Method (Size, Size). MSDN
Size sizestep1 = Size.Subtract(new Size(PictureBox1.Image.Size.Width / 2, PictureBox1.Image.Size.Height / 2), PictureBox1.Size);
Size finalsize = Size.Add(sizestep1, PictureBox1.Image.Size);
// Convert to point.
Point BottomRightCoords = new Point(finalsize.Width, finalsize.Height);
And if you want to get the BottomRightCoords on the form, you have to add the PictureBox Location to it.
A little bit of mathematics suggested above + the code in the following link did the trick:
How to retrieve zoom factor of a WinForms PictureBox?

Scale image to fit to A4 page - Migradoc

I am really struggling to get this right, any help would be appreciated.
I have a series of images that I want to build in to a PDF using MigraDoc (1 image = 1 page)
Each image must be displayed on a separate page but may not extend over the page it must fit on to the page perfectly.
So, how do I scale an image (of any size) to fit to a page using MigraDoc?
You call AddImage() to add the image - and in return you get an Image object that allows you to set width and/or height of the image.
What you have to do: check the dimensions of the image, calculate which is the limiting factor (width or height), then set this limiting factor on the Image object and also set LockAspectRatio.
Or set both Width and Height and leave LockAspectRatio off.
For DIN A4, you may allow e.g. 19 cm x 27.7 cm as maximum image size.
For an image with 1000x1000 pixel you would set the width to 19 cm (assuming LockAspectRatio is on). Height will then also be 19 cm automatically.
For an image with 1000x2000 pixel you would set the height to 27.7 cm. Width will then be 50% of the height.

How can I find the pixel per inch value in a JPG image?

I am trying to validate certain images to not allow images lower than 300 pixels per inch, is there a way to find it on ASP.NET using C#?
You've got to read EXIF data from the image.
Here you have an example of how you can do it, using ExifLib
ExifLib - A Fast Exif Data Extractor for .NET 2.0+
Be warned that not all jpeg images have the resolution information. And, that even if they have it, you can print them using a completely different resolution. I.e. a pic 200px wide can be printed using 1 inch width is 200dpi. This same image printed using 2 inches is 100dpi, and using 1/2 inch is 400dpi.
EDIT: It's even possible to get this info with native .NET framework Image.PropertyItems Property
The Image object of the .NET Framework will give you the PPI of a Bitmap (including a JPG).
Image image = new Bitmap(#"C:\myimage.jgp");
float ppi = image.HorizontalResolution; // the image's pixels per inch
float widthInInches = image.PhysicalDimension.Width / ppi;
Seems to work for me. I was able to discern that a specific image I am using in a PDF is 90 ppi.

Graphics.DrawImage logo scaling incorrectly on a large image

I'm doing some standard code, I am applying a logo to an image and it is working fine.
The source image is always 1024 x 768 as the code before this takes the image and resizes it (creates a new file based on the original).
The logo is applied correctly on some images I have that are 2288 x 1712. If I use an image of 3264 x 2448 then the logo is added at the correct start co ordinates but carries on the x and y axis.
The logo should have a 10px gap between the sides. The 2 letters in the logo that you can see are also far larger than the source image logo.
If I take the image that is doing the wrong behaviour (3264 x 2448) and change it to 2288 x 1712 and then run the code, it outputs the correct result!
I do not understand because the variable sourceImg is always the 1024 x 768 version so why should resizing the original image have an impact?
Image sourceImg = Image.FromFile(Path.Combine(filepath,filename));
Image logo = Image.FromFile(watermark);
Graphics g = Graphics.FromImage(sourceImg);
g.DrawImage(
logo,
sourceImg.Width - horizontalPosition - logo.Width,
sourceImg.Height - verticalPosition - logo.Height
);
g.Dispose();
logo.Dispose();
sourceImg.Save(Path.Combine(filepath, filename));
sourceImg.Dispose();
The overload of the DrawImage that you are using takes the PPI values of the images to calculate the size. As the PPI value of the logo is lower than the PPI value of the image, the logo is scaled up so that their relative physical size (inches instead of pixels) are the same.
Use an overload where you specify the size in pixels:
g.DrawImage(
logo,
sourceImg.Width - horizontalPosition - logo.Width,
sourceImg.Height - verticalPosition - logo.Height,
logo.Width,
logo.Height
);

Categories