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
);
Related
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;
I'm using PDF4NET library to convert image that uploaded by user into a pdf for printing. What i want to achieve is draw the exact same size of image into the pdf. For example, user uploaded a 16px x 16px image, and the pdf will show the same size image at the center.
The code that I'm using is like below:
var canvas = page.Canvas;
var hRatio = page.Width / objImage.Width;
var vRatio = page.Height / objImage.Height;
var ratio = Math.Min(hRatio, vRatio);
page.Canvas.DrawImage(decodedPath, 0, 0, objImage.Width * ratio, objImage.Height * ratio, 0, PDFKeepAspectRatio.KeepWidth);
The problem is when user upload a small image, it will stretch to fit the pdf when draw image.
** The result must be align in center of the pdf
When you draw an image on the PDF page, the drawing size is specified in PDF points. PDF files do not use pixels.
For your situation you should test the 'ratio' and if it is greater than 1 (page is greater than the image) then you should draw the image as it is (no multiplication by ratio).
Disclaimer: I work for the company that develops the PDF4NET library.
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?
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.
I'm writing an application to send some images to a third party, and the images must be 200x200 DPI. The image is a Bitmap and is sized at 500 width and 250 height.
The first time I tested the images with the third party, my resolution was incorrect. I merely used image.SetResolution(200,200) to correctly set it to 200x200. This, however, only changed the resolution tag for the image and did not properly, according to my third party technical contact, adjust the image height and width.
Is there a ratio that I can use so that for each X units I increment the resolution, I merely increment the corresponding height or width Y units? I thought that I could just increment resolution without having to increment height or width.
Thank you,
Aaron.
An image stored digitally has no meaningful concept of DPI. DPI comes into play when reproducing an image on a physical device.
You need to adjust the image size with regard to the DPI of the physical device, and the desired size of the output on that device.
For example, if a printer tells you they need an image at 300dpi to fill a space of 4in x 4in then you would provide them a bitmap with a size of 1200x1200 pixels. This image would end up with a physical size of 4in x 4in on a 300dpi output device. On a 600dpi device the same image would have an output size of 2in x 2in.
When dealing with digital images, you usually refer to PPI, which is pixels per inch. DPI is not directly related to digital image resolution.
So, if you look at a image that is 200px by 200px # 200PPI, you will have an image that is 1 inch by 1 inch.