c# - How to resize image without losing quality -


hey have image:

i using method resize images:

public static bitmap resizeimage(image image, int width, int height) {     var destrect = new rectangle(0, 0, width, height);     var destimage = new bitmap(width, height);      destimage.setresolution(image.horizontalresolution, image.verticalresolution);      using (var graphics = graphics.fromimage(destimage))     {         graphics.compositingmode = compositingmode.sourcecopy;         graphics.compositingquality = compositingquality.highquality;         graphics.interpolationmode = interpolationmode.highqualitybicubic;         graphics.smoothingmode = smoothingmode.highquality;         graphics.pixeloffsetmode = pixeloffsetmode.highquality;          using (var wrapmode = new imageattributes())         {             wrapmode.setwrapmode(wrapmode.tileflipxy);             graphics.drawimage(image, destrect, 0, 0, image.width, image.height, graphicsunit.pixel, wrapmode);         }     }      return destimage; } 

however when i'm done result:

as can see picture somehow dotty , poor quality. changed approach , used method resize picture:

public static image resizeimage(image originalimage, size thumbsize) {     int32 thwidth = thumbsize.width;     int32 thheight = thumbsize.height;     image = originalimage;     int32 w = i.width;     int32 h = i.height;     int32 th = thwidth;     int32 tw = thwidth;     if (h > w)     {         double ratio = (double)w / (double)h;         th = thheight < h ? thheight : h;         tw = thwidth < w ? (int32)(ratio * thwidth) : w;     }     else     {         double ratio = (double)h / (double)w;         th = thheight < h ? (int32)(ratio * thheight) : h;         tw = thwidth < w ? thwidth : w;     }     bitmap target = new bitmap(tw, th);     graphics g = graphics.fromimage(target);     g.smoothingmode = smoothingmode.highquality;     g.compositingquality = compositingquality.highquality;     g.interpolationmode = interpolationmode.high;     rectangle rect = new rectangle(0, 0, tw, th);     g.drawimage(i, rect, 0, 0, w, h, graphicsunit.pixel);     return (image)target; } 

but issue still stands. wondering how may able resize image smaller size without losing quality.

i must add after resize create byte array , save within database (yes know bad thing, within project has saved within database). on retrieval image webapi, byte array converted base64 string. , show b64 on image tag below. e.g:

<img src="data:image/png;base64, ivborw0kggoaaaansuheugaaaaua aaafcayaaacnbyblaaaaheleqvqi12p4//8/w38giaxdibke0dhxgljnbaao     9txl0y4ohwaaaabjru5erkjggg=="/> 

i'm using following method thousands of images , never loses significant quality or results in dotted image.

public static image scaleimage(image image, int height) {     double ratio = (double)height/ image.height;     int newwidth = (int)(image.width * ratio);     int newheight = (int)(image.height * ratio);     bitmap newimage = new bitmap(newwidth, newheight);     using (graphics g = graphics.fromimage(newimage))     {         g.drawimage(image, 0, 0, newwidth, newheight);     }     image.dispose();     return newimage; } 

i've taken liberty of scaling image posted using code 128px (like thumbnail posted).

result:

enter image description here


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -