I have image in folder image
how resize image with imagebox in emgucv at open image?
thnx..
// To get orginal image from the OpenFileDialog
Image<Bgr, Byte> captureImage = new Image<Bgr, byte>(openImageFileDialog.FileName);
// To resize the image
Image<Bgr, byte> resizedImage = captureImage.Resize(width, height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
Hope it helps.
The answer is very simple.
Let us suppose the path of image is "C:\image.jpg".
Mat frame = new Mat(); //Declaration
string path = #"C:\image.jpg";
int width = 640, height = 480;
frame = CvInvoke.Imread(path , LoadImageType.AnyColor);
CvInvoke.Resize(frame, frame, new Size(imagebox1.Width, imagebox1.Height), 0, 0, Inter.Linear); //This resizes the image to the size of Imagebox1
OR
CvInvoke.Resize(frame, frame, new Size(width, height), 0, 0, Inter.Linear); //This resizes the image into your specified width and height
This is How i resized image using EmguCV
Bitmap bitmap = new Bitmap(FileUpload1.PostedFile.InputStream);
Image<Hsv, Byte> Iimage = new Image<Hsv, byte>(bitmap);
Image<Hsv, Byte> HsvImage = Iimage.Resize(384, 256,INTER.CV_INTER_LINEAR);
Related
I can't center on table cell the image using iTextSharp.
I have tried in this mode, without success.
The image is always misaligned with respect to the cell and covers the upper cell in the pdf file.
How to do resolve this?
My code below.
string imagePath = "~/aspnet/Img/pdf.gif";
iTextSharp.text.Image image2 = iTextSharp.text.Image.GetInstance(imagePath);
image2.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
Chunk cImage = new Chunk(image2, 0, 0, false);
Anchor anchor = new Anchor(cImage);
anchor.Reference = "www.mywebsite.com";
table.AddCell(anchor);
Please, try this.
You must use the property Annotation of iTextSharp.
string imagePath = "~/aspnet/Img/pdf.gif";
iTextSharp.text.Image image2 = iTextSharp.text.Image.GetInstance(imagePath);
PdfPCell cell2 = new PdfPCell(image2);
cell2.HorizontalAlignment = Element.ALIGN_LEFT;
cell2.VerticalAlignment = Element.ALIGN_MIDDLE;
cell2.FixedHeight = 20;
image2.Annotation = new Annotation(0, 0, 0, 0, "www.mywebsite.com");
table.AddCell(cell2);
I am using the code below to resize an image in Swift on macOS. This is working but if the image is not square to begin with, the resizing squashes the image.
How can I resize the image but draw it in the center and keeping the ratio, preventing the squashing if the image is not square to begin with?
func resize(image: NSImage, w: CGFloat, h: CGFloat) -> NSImage {
var destSize = NSMakeSize(CGFloat(w), CGFloat(h))
var newImage = NSImage(size: destSize)
newImage.lockFocus()
image.draw(in: NSMakeRect(0, 0, destSize.width, destSize.height), from: NSMakeRect(0, 0, image.size.width, image.size.height), operation: NSCompositingOperation.sourceOver, fraction: CGFloat(1))
newImage.unlockFocus()
newImage.size = destSize
return NSImage(data: newImage.tiffRepresentation!)!
}
Thank you
The following line of code is the problem:
image.draw(in: NSMakeRect(0, 0, destSize.width, destSize.height), from: NSMakeRect(0, 0, image.size.width, image.size.height), operation: NSCompositingOperation.sourceOver, fraction: CGFloat(1))
You are drawing from the whole rectangle of the source into the whole rectangle of the destination. This will therefore scale the image to fit the destination, whereas you want to maintain aspect ratio. You need to decide how you want the final image to appear and adjust the source or destination rectangles accordingly.
For example, if you want to scale the result so that the whole image appears, you need to adjust either the width or height of the destination rectangle so they're in the same aspect ratio as the source.
Alternatively, if you want to crop the result, you need to adjust the width or height of the source rectangle, once again maintaining the same aspect ratio. You will also have to adjust the origin of the source rectangle if you want to crop (for example) the top and bottom.
Some of the other functions like draw(in:from:operation:fraction:) handle scaling for you, so depending on your needs, they may be better.
Assuming that you want to fit the entire source image into the destination image space(or size), maintaining the aspect ratio of the original image and adding some white space where the image doesn't fit into the destination.
There are three situations to consider.
The aspect ratio of the source and destination are the same.
No problem, just resize.
The aspect ratio destination is wider than the source
Height is the driving value (because it is smaller).
Figure out the height ratio from source to destination.
Use that ratio to calculate the destination width from the source width.
The aspect ratio destination is taller than the source
Width is the driving value.
Figure out the width ratio from source to destination.
Use that ratio to calculate the destination height from the source height.
I created this function to calculate the size you need.
func calcNewSize(source: NSSize, destination: NSSize) -> NSSize {
let widthRatio: Float = Float(destination.width) / Float(source.width)
let heightRatio: Float = Float(destination.height) / Float(source.height)
var newSize = NSSize()
print("widthRatio \(widthRatio) heightRatio \(heightRatio)")
if widthRatio == heightRatio {
print("use same ratio")
newSize = destination
}
else if widthRatio > heightRatio {
print("use height ratio")
newSize.height = source.height * CGFloat(heightRatio)
newSize.width = source.width * CGFloat(heightRatio)
}
else {
print("use width ratio")
newSize.height = source.height * CGFloat(widthRatio)
newSize.width = source.width * CGFloat(widthRatio)
}
return newSize
}
I found this extension online, it allows me to have images adhere to aspect fit/fill even when drawn inside dynamically growing/shrinking image views (currently when image is saved to camera roll after my draw function the image reverts to "scale fill" regardless of what the content mode of the image view is. I suspect the reasoning for this is because I have it drawing the image to size/bounds of the image view, but since the image view is dynamic, i don't see any way around this without using this extension):
// MARK: - Image Scaling.
extension UIImage {
/// Scales an image to fit within a bounds with a size governed by the passed size. Also keeps the aspect ratio.
/// Switch MIN to MAX for aspect fill instead of fit.
///
/// - parameter newSize: newSize the size of the bounds the image must fit within.
///
/// - returns: a new scaled image.
func scaleImageToSize(newSize: CGSize) -> UIImage {
var scaledImageRect = CGRect.zero
let aspectWidth = newSize.width/size.width
let aspectheight = newSize.height/size.height
let aspectRatio = max(aspectWidth, aspectheight)
scaledImageRect.size.width = size.width * aspectRatio;
scaledImageRect.size.height = size.height * aspectRatio;
scaledImageRect.origin.x = (newSize.width - scaledImageRect.size.width) / 2.0;
scaledImageRect.origin.y = (newSize.height - scaledImageRect.size.height) / 2.0;
UIGraphicsBeginImageContext(newSize)
draw(in: scaledImageRect)
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage!
}
}
This is my current function I'm using for drawing the image on screen to be able to save it to camera roll (this function combines two images, a frame and an image from camera roll:
func drawImagesAndText() {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: imageView.bounds.size.width, height: imageView.bounds.size.height))
img = renderer.image { ctx in
// var newSize = currentImage.scaleImageToSize
let bgImage = currentImage
bgImage?.draw(in: CGRect(x: 0, y: 0, width: imageView.bounds.size.width, height: imageView.bounds.size.height))
frames = UIImage(named: framesAr)
frames?.draw(in: CGRect(x: 0, y: 0, width: imageView.bounds.size.width, height: imageView.bounds.size.height))
}
}
All the tutorials I've found on how to use extensions don't cover how to pass in and out variables like this one requires. Any insight would be greatly appreciated.
I understand that you don't know how to use the extension, is that correct? Since it just adds a function to every UIImage, you can simply call it on your image like this: currentImage.scaleImageToSize(newSize: someSize) and pass the size you want the image to fit into.
Dorian Roy was telling me to use that call in place of using just "currentImage", and that's what worked!
(I commented on his initial answer saying I was having issues because I was trying to use the return value from the extension itself in place of "currentImage")
I assign an image to the background so the user can draw on it but the image always is not in the right size it takes like 1/3 of it and assigns it to the image view
the only way to bypass this is to make a canvas exactly the size of the image assigned like in PICSART app,
does any one knows how to do this?
mainimageview.backgroundColor = UIColor(patternImage: "draw")
`
You can write your own method to resize the image you want at the time of assigning background color. Pass your image and size you want
func imageWithImage(image:UIImage?, newSize:CGFloat)->UIImage?
{
let size = CGSizeMake(newSize, newSize)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
image?.drawInRect(CGRectMake(0, 0, size.width, size.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
I have used a C# method to resize the image size.I have used several methods from the following links.
C# Simple Image Resize : File Size Not Shrinking
shrinking
Resize an Image C#
but quality is missing when re-sizing the image
Please help me to resolve the issue.
You could use WIC (Windows Imaging Component) instead of GDI+. Here's a nice blog post with an example.
First convert your Bitmap to an Image
Bitmap b = new Bitmap("asdf.jpg");
Image i = (Image)b;
then pass the image to this method to resize
public static Image ResizeImage(Image image, int width, int height, bool onlyResizeIfWider)
{
using (image)
{
// Prevent using images internal thumbnail.
image.RotateFlip(RotateFlipType.Rotate180FlipNone);
image.RotateFlip(RotateFlipType.Rotate180FlipNone);
if (onlyResizeIfWider == true)
if (image.Width <= width)
width = image.Width;
// Resize with height instead?
int newHeight = image.Height * width / image.Width;
if (newHeight > height)
{
width = image.Width * height / image.Height;
newHeight = height;
}
Image NewImage = image.GetThumbnailImage(width, newHeight, null, IntPtr.Zero);
return NewImage;
}
}
I hope this helps.