How to keep aspect ratio in panel C#? -


i save image in preview mode. preview mode contains picturebox , label control.

the problem when save image. recorded screen.

the export image different expect, not keep aspect ratio. so, control in panel after save example.jpg wrong position.

my code use scaleimage:

public static image scaleimage(image image, int maxwidth, int maxheight) {     var ratiox = (double) maxwidth/image.width;     var ratioy = (double) maxheight/image.height;     var ratio = math.min(ratiox, ratioy);      var newwidth = (int) (image.width*ratio);     var newheight = (int) (image.height*ratio);      var newimage = new bitmap(newwidth, newheight);     graphics.fromimage(newimage).drawimage(image, 0, 0, newwidth, newheight);     return newimage; } 

my code save image:

graphics g = graphics.fromimage(img); string s = lstimgadded.items[k].text; bitmap bm = new bitmap(@"" + s); panel2.backgroundimage = bm; pointf p1 = stretchimagesize(postpoint, panel2); g.drawimage(     drawtext(lstaliasimage[i - valuesfrom], fonttype, colorinput,         color.transparent),     point.round(stretchimagesize(postpoint, panel2))); // point.round(stretchimagesize(postpoint, panel2)) ở đây dùng nhìu lần  g.drawimage(ctrl.image, point.round(stretchimagesize(postpointpicturebox, panel2)).x, point.round(stretchimagesize(postpointpicturebox, panel2)).y,     ctrl.width, ctrl.height); // panel2 có phải cái hình nhỏ ko? ko. picturebox moi la nho, panel la background lon   g.dispose(); string linklocation = txtaddress.text; scaleimage(img, witdhimg, heightimg)     .save(linklocation + "\\" + lstaliasimage[i - valuesfrom] + "." + imgtype,         imageformat.jpeg); 

and class stretchimage scale control(picturebox, label) a panel. image show before image in panel different after save.

image in software

in software, image shown in panel. not scale in preview software: after in save in computer

applying aspect ratio windows forms

much intercepted windows message in our windows 7 style form, keep aspect ratio of windows form going intercept resizing windows messages. done overriding wndproc.  here list of constants need algorithm:  wm_sizing = 0x214 wmsz_left = 1 wmsz_right = 2 wmsz_top = 3 wmsz_bottom = 6 
  1. the first 1 way our c# application can tell when form being resized. rest of constants tell side of form being resized. note corners edges combine. example, lower left corner resizing right , bottom parts of form , 2 + 6 = 8.

  2. the advantage of using wndproc instead of windows form events can prevent form flickering. resize events in .net called until after resizing has been applied, modifying size in event produces flickering.

  3. make sure download sample c# application. notice how no matter resize form from, aspect ratio maintained. quick peek @ source code shows how either height or width adjusted depending on side of form being resized. cool thing form can still maximized fill entire screen without problems.

try this

public void resizeimage(string originalfile, string newfile, int newwidth, int maxheight, bool onlyresizeifwider) { system.drawing.image fullsizeimage = system.drawing.image.fromfile(originalfile); // prevent using images internal thumbnail fullsizeimage.rotateflip(system.drawing.rotatefliptype.rotate180flipnone); fullsizeimage.rotateflip(system.drawing.rotatefliptype.rotate180flipnone); if (onlyresizeifwider) { if (fullsizeimage.width  maxheight) { // resize height instead newwidth = fullsizeimage.width * maxheight / fullsizeimage.height; newheight = maxheight; } system.drawing.image newimage = fullsizeimage.getthumbnailimage(newwidth, newheight, null, intptr.zero); // clear handle original file can overwrite if necessary fullsizeimage.dispose(); // save resized picture newimage.save(newfile); } 

Comments