c# - Use TrackBar to Zoom In/out of Picturebox Proportionately -
i'm using winforms. in form have picturebox want zoom in , out using track bar. picturebox set zoom-mode. want image , picturebox proportion height/width when drag bar. how can accomplish this?
private void open_btn_click(object sender, eventargs e) { openfiledialog openfiledialog1 = new openfiledialog(); if(openfiledialog1.showdialog() == dialogresult.ok) { image bmp; bmp = new bitmap(openfiledialog1.filename); if (bmp == null) { messagebox.show("loading image failed", "error", messageboxbuttons.yesno, messageboxicon.warning); } else { picturebox1.image = bmp; openfiledialog1.dispose(); } } } private void zoomslider_scroll(object sender, eventargs e) { if(trackbar1.value == 1) { picturebox1.height += 50; picturebox1.width += 50; } if(trackbar1.value == 2) { picturebox1.height += 100; picturebox1.width += 100; } if(trackbar1.value == 3) { picturebox1.height += 200; picturebox1.width += 200; } //this not had in mind... }
when form created have save size
form1 : form { private size _pictoriginalsize; form1() { initialisecomponent(); _pictoriginalsize = picturebox1.size; zoomslider.minimum = 0; zoomsldier.maximum = 1000; ... }
now know it's unzoomed size was.
next need need convert value of slider into scale factor.
private void zoomslider_scroll(object sender, eventargs e) { const double maxscale = 5.0; // scale factor when @ it's max double scale = math.pow(maxscale, trackbar1.value / trackbar1.maximum); size newsize = new size((int) (_pictorignalsize.width * scale), (int) (_pictorignalsize.height * scale)); picturebox.size = newsize; }
i using math.pow
function convert scale 1 5 on exponential scale - may want use different technique consider
- having slider have values 1 5 , value becomes scale:
scale = zoomslider.value
- gives resoultion of 1. - having slider have values 1 5000 , double division
double scale = zoomslider.value / 1000.0;
gives 1000 different resolutions.
nb: using 5 example - can use value max scale factor.
Comments
Post a Comment