C# - Resize image canvas (keeping original pixel dimensions of source image) -
my goal take image file , increase dimensions next power of 2 while preserving pixels (aka not scaling source image). end result original image, plus additional white space spanning off right , bottom of image total dimensions powers of two.
below code i'm using right now; creates image correct dimensions, source data scaled , cropped reason.
// load image , determine new dimensions system.drawing.image img = system.drawing.image.fromfile(srcfilepath); size szdimensions = new size(getnextpwr2(img.width), getnextpwr2(img.height)); // create blank canvas bitmap resizedimg = new bitmap(szdimensions.width, szdimensions.height); graphics gfx = graphics.fromimage(resizedimg); // paste source image on blank canvas, save .png gfx.drawimageunscaled(img, 0, 0); resizedimg.save(newfilepath, system.drawing.imaging.imageformat.png);
it seems source image scaled based on new canvas size difference, though i'm using function called drawimageunscaled(). please inform me of i'm doing wrong.
the method drawimageunscaled
doesn't draw image @ original pizel size, instead uses resolution (pixels per inch) of source , destination images scale image it's drawn same physical dimensions.
use drawimage
method instead draw image using original pixel size:
gfx.drawimage(img, 0, 0, img.width, img.height);
Comments
Post a Comment