c# - Panel and ScrollBar in Windows forms -


i using panel display image in windows forms drawing image in panel in panel_paint event follows:

graphics g = panel1.creategraphics(); image im = new bitmap(@"../../data/#3_page2.png"); g.drawimage(im,new point(10,10)); 

now, image drawn expected, part of bottom of image not displaying height greater forms height. have added vscrollbar now. how make panel view rest of image of vscrollbar.

this solution works, if image large enough, there little flicker when scroll (however it's acceptable). first have add vscrollbar right on right of panel , hscrollbar right under bottom of panel. demo requires have vscrollbar named vscrollbar1 , hscrollbar named hscrollbar1, button named buttonopenimage allow user open image, panel named panel1 used main area draw image:

public partial class form1 : form {     public form1()     {         initializecomponent();                     //to prevent/eliminate flicker,         typeof(panel).getproperty("doublebuffered",                                    system.reflection.bindingflags.instance |                 system.reflection.bindingflags.nonpublic).setvalue(panel1, true, null);         //------------------------         updatescrollbarsstate();     }             int imgwidth, imgheight;     image image;     point lefttop = point.empty;     int lastv, lasth;     private void openimage(){         openfiledialog openfile = new openfiledialog();                     openfile.fileok += (s, e) => {             try {                 image = image.fromfile(openfile.filename);                 //calculate physical size of image based on resolution , logical size.                 //we have because drawimage based on physical size (not logical).                 imgwidth = (int)(image.width * 96f / image.horizontalresolution + 0.5);                 imgheight = (int)(image.height * 96f / image.verticalresolution + 0.5);                 lastv = lasth = 0;                 updatescrollbarsstate();                 vscrollbar1.value = 0;                 hscrollbar1.value = 0;                 panel1.invalidate();             }             catch {                 image = null;                 messagebox.show("image file invalid or corrupted!");             }         };         openfile.showdialog();     }     private void updatescrollbarsstate() {         //we have update info minimum , maximum         vscrollbar1.minimum = 0;         hscrollbar1.minimum = 0;         vscrollbar1.maximum = math.max(imgheight-panel1.height,0);         hscrollbar1.maximum = math.max(imgwidth-panel1.width,0);         vscrollbar1.visible = vscrollbar1.maximum > 0;         hscrollbar1.visible = hscrollbar1.maximum > 0;         if (vscrollbar1.maximum == 0) {             lefttop.y = 0;             lastv = 0;         }         if (hscrollbar1.maximum == 0) {             lefttop.x = 0;             lasth = 0;         }     }     private void panel1_paint(object sender, painteventargs e) {         if (image == null) return;         e.graphics.drawimage(image, lefttop);     }     //the valuechanged event handler of vscrollbar1     private void vscrollbar1_valuechanged(object sender, eventargs e) {         if (!vscrollbar1.visible) return;         lefttop.offset(0, -vscrollbar1.value + lastv);         lastv = vscrollbar1.value;         panel1.invalidate();     }     //the valuechanged event handler of hscrollbar1     private void hscrollbar1_valuechanged(object sender, eventargs e) {         if (!hscrollbar1.visible) return;         lefttop.offset(lasth - hscrollbar1.value, 0);         lasth = hscrollbar1.value;         panel1.invalidate();     }     //handler sizechanged event of panel. if resizing     //the form causes panel's size changing, should attach      //handler form.     private void panel1_sizechanged(object sender, eventargs e) {         updatescrollbarsstate();     }     //handler click event of button (click open image)     private void buttonopenimage_click(object sender, eventargs e) {         openimage();     } } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -