How to Resize an Image That Is Being Printed

How to resize an image that is being printed?

As method resizeImage is returning new UIImage you need store result of

self.resizeImage(img, targetSize: CGSizeMake(200.0, 200.0))

in another object like:

let resizedImage = self.resizeImage(img, targetSize: CGSizeMake(200.0, 200.0))

And now you can use this resizedImage image for print like:

printController.printingItem = resizedImage

Hope this will help.

EDIT:

Below is update code:

func printImage(img:UIImage) {

let printController = UIPrintInteractionController.shared

let printInfo = UIPrintInfo(dictionary:nil)
printInfo.jobName = "Printing 's QR code"
printInfo.outputType = .photoGrayscale

if let resizedImage = self.resizeImage(image: img, targetSize: CGSize(width: 200, height: 200)) {

printController.printInfo = printInfo
printController.printingItem = resizedImage

printController.present(animated: true) { (_, isPrinted, error) in
if error == nil {
if isPrinted {
print("QR Code Has Been Printed :)")
} else {
print("QR Code Not Printed")
}
}
}
}
}

func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {

let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height

// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}

// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage
}

Resizing image while printing html

You can control the layout of the print document by using mediaqueries. An example would be

@media print { 
img {
max-width : 300px;
height : auto;
}
}

more information about this and some examples can be found here

Issues Resizing Image to fit on a printed page

I did not find a solution to this problem. I worked around it by using the printer margins when performing a print preview and ignoring the margins (start at 0,0 origin) when actually printing. I believe that this is possibly a bug in the printer driver? But I can't confirm.

Rescale an image for printing

Try with that:

g2.drawImage(createImage(temperaturePlot).getScaledInstance(500, 300,BufferedImage.SCALE_SMOOTH), 85, 65, null);

I think this solve your problem.

how to resize a image being printed over another image (swift3)

This function might help you. You can edit the size and origin values as per your wish

func drawImage() -> UIImage {
var bottomImage = UIImage(named: "red")
//background image
let image: UIImage = UIImage(named: "blue")
//foreground image
let newSize: CGSize = CGSize(width: 80, height: 80)
UIGraphicsBeginImageContext(newSize)

bottomImage.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))

image.draw(in: CGRect(x: 20, y: 20, width: 40, height: 40), blendMode: .normal, alpha: 1.0)
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

return newImage
}

how to resize an image for printing in php

If you want to resize your image on printing you can create a print.css. Include it in your html page with:

<link rel="stylesheet" href="print.css" screen="print">

..and then add your style property for the image that you want to be resized in print.css

img.yourimage
{
width: newwidth;
height: newheight;
}


Related Topics



Leave a reply



Submit