c# - How can i resize images by selecting/changing the images kb ? And not resolution -
this how i'm using it:
bitmap bmp1 = new bitmap(resizeimages(filename,100,100));
but i'm changing images resolution in example 100,100 want change images size kb mean if file size 3.64mb or example 3,227kb , know in site want upload images maximum size of single image can upload 1024k want able change image size 1024k.
i want make resizeimages instead adding resolution 100,100 add size resizeimages(filename,1024);
this resizeimages method maybe it's better make new method ?
private static bitmap resizeimages(string filename, int maxwidth, int maxheight) { using (image originalimage = image.fromfile(filename)) { //caluate new size int newwidth = originalimage.width; int newheight = originalimage.height; double aspectratio = (double)originalimage.width / (double)originalimage.height; if (aspectratio <= 1 && originalimage.width > maxwidth) { newwidth = maxwidth; newheight = (int)math.round(newwidth / aspectratio); } else if (aspectratio > 1 && originalimage.height > maxheight) { newheight = maxheight; newwidth = (int)math.round(newheight * aspectratio); } if (newwidth >= 0 && newheight >= 0) { bitmap newimage = new bitmap(newwidth, newheight); using (graphics g = graphics.fromimage(newimage)) { //--quality settings adjust fit application g.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybilinear; g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality; g.pixeloffsetmode = system.drawing.drawing2d.pixeloffsetmode.highquality; g.compositingquality = system.drawing.drawing2d.compositingquality.highquality; g.drawimage(originalimage, 0, 0, newimage.width, newimage.height); return newimage; } } return null; }
update
i found 2 methods , it's working didn't understand how it's working size change:
private static imagecodecinfo getencoderinfo(string mimetype) { int j; imagecodecinfo[] encoders; encoders = imagecodecinfo.getimageencoders(); (j = 0; j < encoders.length; ++j) { if (encoders[j].mimetype == mimetype) return encoders[j]; } return null; } private static void savejpgwithcompressionsetting(image image, string szfilename, long lcompression) { encoderparameters eps = new encoderparameters(1); eps.param[0] = new encoderparameter(system.drawing.imaging.encoder.quality, lcompression); imagecodecinfo ici = getencoderinfo("image/jpeg"); image.save(szfilename, ici, eps); }
in constructor did:
bitmap bmp1 = new bitmap(filename); savejpgwithcompressionsetting(bmp1, filepath, 10l); bmp1.dispose();
what 10l mean ?
in case example filename original size 6.71mb new reszied file 566kb
and if want make file size 1024k example ?
so use following code crate jpeg file:
bitmap bmp1 = new bitmap(filename); savejpgwithcompressionsetting(bmp1, filepath, 10l); bmp1.dispose();
the 10l
used encoder.quality, number 0 100, 100 highest quality. l
in 10l
means long
, , not int
. example above write jpeg quality=10, ie 10%.
as 10% low quality, see not good. 75% more common value. 10% reduced filesize 6.71mb 566kb, lot. when use 75%, file larger.
as require 1024k, need find correct quality results in filesize, or bit smaller, cannot go on limit. there no easy way determine quality need. have try it.
so example, start 50%, file still large, try 25%. continue way until find best value. best if try in-memory. i'm not sure possible. otherwise have write images disk, , check filesize.
it depends on image how filesize reduced quality. example screenshot harder compress (using jpeg) nature photograph. each picture have different quality setting when filesize if 1024k.
Comments
Post a Comment