Why is this code shrinking my image? - asp.net

I've take some code and reduced it down to a few lines which still reproduce the error i am having. In this case, I am taking an image that is 448x298 in size and trying to overlay it on top of a white background that's 600x450.
So I'm expecting to get an image that's 600x450 with a white background and my original image laid on top of it starting in the upper right corner. And i expect my original image to remain it's original size. Instead the original image is going from 448x298 to approximately (give or take a pixel or two) 143x95
Here is the reduced code that's doing this:
System.Drawing.Image oImage = new Bitmap(600, 450);
Graphics oGraphic = Graphics.FromImage(oImage);
oGraphic.FillRectangle(Brushes.White, 0, 0, 600, 450);
oGraphic.DrawImage(image, new Point(0,0));
return (Bitmap)oImage;

You have to specify the target size. The overload you chose scales the image from the source dpi to the target dpi. As explained in another question, you should do this:
System.Drawing.Image oImage = new Bitmap(600, 450);
Graphics oGraphic = Graphics.FromImage(oImage);
oGraphic.FillRectangle(Brushes.White, 0, 0, 600, 450);
oGraphic.DrawImage(image, 0,0, image.Width, image.Height);
return (Bitmap)oImage;

Related

Enlarge Image Canvas

Is there a quick way using System.Drawing to quickly enlarge the image canvas of an .png image? (see example below). The caveat is the background might be transparent and I want to keep it transparent.
Edit: Needs to be in ASP .Net CORE
Alternatively, is there a way of putting the image on a white background that is slightly larger than the image?
After a few days of trial and error, I think I found something that works
Image overlayImage = //get your image here from file or url.
xloc = //x coord where to start overlay image.
yloc = //y coord where to start overlay image.
canvasWidth = //width of background canvas
canvasHeight = //height of background canvas
Bitmap baseImage = new Bitmap(canvasWidth, canvasHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics graphics = Graphics.FromImage(baseImage))
{
using (System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White))
{
graphics.FillRectangle(myBrush, new Rectangle(0, 0, canvasWidth, canvasHeight)); // white rectangle
}
graphics.CompositingMode = CompositingMode.SourceOver;
graphics.DrawImage(overlayImage, xloc, yloc);
} // graphics will be disposed at this line

Compose one image from multiple images in asp.net and crop them

I am making a system for compositioning an image from multiple images. Let's say I have two images and I want to compose an image like this one :
The thick border defines the complete image that I want. It is a div with the resolution of 600X540 and inside of it two divs, each one containing a different image that I can position as I want. At the end of the positioning, I get the left and top borders of each image (corresponding to the whole div).
I'm having trouble creating the image in code behind. I'm using the Bitmap.DrawImage Method. Here is my code :
oImgOut = New System.Drawing.Bitmap(600, 540)
oGraph = System.Drawing.Graphics.FromImage(oImgOut)
oGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
oGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
oGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
oGraph.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality
oGraph.FillRectangle(Drawing.Brushes.White, 0, 0, 600, 540)
//image 1
oGraph.DrawImage(oImg1, CInt(left1), CInt(top1), CInt(width1), CInt(height1))
oImg1.Dispose()
//image2
oGraph.DrawImage(oImg2, CInt(left2), CInt(top2), CInt(width2), CInt(height2))
oImg2.Dispose()
oImgOut.Save(strDirectory)
oImgOut.Dispose()
This is what I have got so far :
I tried to give each image a width of 300px and height of 540px, but as you see the image on the right looks ugly :
I was wondering if there is a way to crop the image? I looked over most of the DrawImage prototypes but I couldn't find any that do the job I want. Surely you'll know better than me.
Can anyone help me?
I found a solution for cropping images with the DrawImage function
oGraph.DrawImage(oImg2, CInt(left2), CInt(top2), _
New Rectangle(0, 0, _
If(CInt(width2) > 600, 600, CInt(width2)), _
If(CInt(height2) > 270, 270, CInt(height2)) + CInt(top2) _
), GraphicsUnit.Pixel)
but i don't understand how it works, please guys i need your help :( .

Resize existing JavaFX image

I have to load images:
Image image = new Image(f.toURI().toString(), width, height, true, true);
Since I also have tiffs, I have to load them differently using JAI:
BufferedImage read = ImageIO.read(f);
Image image = SwingFXUtils.toFXImage(read, null);
Now I have an Image object, but it has the wrong size. Image offers no methods to resize the object. How do I resize is so it has the same size as if I would load a jpg or png with the shown line?
Seems like there should be an easier way to do this, but you can try:
BufferedImage read = ImageIO.read(f);
int[] pixels = new int[width * height] ;
PixelGrabber grabber = new PixelGrabber(read.getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, width, height, 0, pixels, width);
grabber.grabPixels();
WritableImage fxImage = new WritableImage(width, height);
PixelWriter pw = fxImage.getPixelWriter();
pw.setPixels(0, 0, width, height, PixelFormat.getIntArgbInstance(), pixels, 0, width);
Now fxImage is a javafx.scene.image.Image that has dimensions width by height.
Be aware that PixelGrabber.grabPixels() is a blocking call, so you will need to handle InterruptedExceptions. For large images you might want to do this in a Task executed on a background thread, so as not to block the FX Application Thread.

How can an image be cropped based on a set scale of random image?

Working on allowing the upload of images which can range in a variety of size, then allowing to crop a predefined area of the image for a thumbnail.
The thumbnail size is predefined to 150x150. Using the Jcrop.js tool to select a section of the image.
Problem:
When displaying the uploaded image in a smaller size than the original image by implementing set height/width on the image rendered, then there is a scale factor that comes into play when selecting an area to crop.
You either have to scale down the cropping area proportionately or you have to scale the image in relation to the actual image's size in comparison to its displayed size.
Question:
How do I figure out the scale of the browser displayed image vs. original image? I am currently using the following code to save the image, but how would I take into consideration the scaling?
public static Image CropImage(Image originalImage, int x, int y, int width, int height)
{
var bmp = new Bitmap(width, height);
bmp.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
using (var graphic = Graphics.FromImage(bmp))
{
graphic.SmoothingMode = SmoothingMode.AntiAlias;
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.PixelOffsetMode = PixelOffsetMode.HighSpeed;
graphic.DrawImage(originalImage, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
return bmp;
}
}
Bonus Question:
Another problem I discovered, is that there seems to be no efficient way to transfer the original file's ImageFormat when creating a new Bitmap which creates a ImageFormatMemoryBMP and when you attempt to call Bitmap.Save(memorystream, original rawformat) it will blow up. And bitmap RawFormat has no setter.
So how can you set the format on a new bitmap?
I think perhaps that this problem is solved purely on the front end, no need to use any server side for this.
Jcrop has a built in scale factor handler.
http://deepliquid.com/content/Jcrop_Sizing_Issues.html
Now you can use this in two ways, as I understand it. Either to 'resize' the image for you on the front end using 'box sizing', or you can tell it the 'truesize' of the image and it will work out the scale factor and handle the coordinates for you on it's own.
Box sizing
$('#cropbox').Jcrop({ boxWidth: 450, boxHeight: 400 });
True Size
$.Jcrop('#cropbox',{ trueSize: [500,370] });
Using the true size method you will need to invoke jcrop using the api method:
http://deepliquid.com/content/Jcrop_API.html#API_Invocation_Method
var jcrop_api,
options = { trueSize: [500,370] };
$('#target').Jcrop(options,function(){
jcrop_api = this;
});
Good luck!

How to add an image on the top of another image?

I want to show difference between a trimed clip and non trimed clip in my video editor application, i.e. I want to add a small film image on my thumbnail for a trimed clip. How can I do this?
It would be just to show the difference between an image and a video in our gallery application.
How to add an image on the top of another one in Qt?
Open the QPainter on the bottom image and draw the top image using its drawPixmap()/drawImage() methods.
QPixmap base, overlay; // come from your code
{
QPainter painter(base);
painter.drawPixmap(100, 100, overlay);
}
If your overlay contains an alpha channel (e.g. fancy PNG icon) and your base image does not, you should create a new QPixmap with an alpha channel and draw both images into it:
QPixmap base, overlay; // come from your code
QPixmap result(base.width(), base.height());
result.fill(Qt::transparent); // force alpha channel
{
QPainter painter(&result);
painter.drawPixmap(0, 0, base);
painter.drawPixmap(100, 100, overlay);
}
QPixmaps and QImages can be used interchangeably, although not all combinations give good performance).
If it's just about showing an image above another, then you could also go with this answer.
QGridLayout *layout = new QGridLayout(widget);
Pixmap base, overlay;
QLabel *background = new Label();
background->setPixmap(&base);
QLabel *lOverlay = new QLabel();
lOverlay->setPixmap(&overlay);
//label gets positioned above textBrowser and is an overlay
layout->addWidget(background, 0, 0, Qt::AlignLeft | Qt::AlignTop);
layout->addWidget(lOverlay, 0, 0, Qt::AlignRight | Qt::AlignBottom);
Of course then the QPixbuf of the background doesn't contain the QPixbuf of the overlay-image, but it only appears to do.

Resources