c# - Panel displays scaled images -
i using code draw scrollable panel on win form. imagebox 512x512 , image using 1024x768 (added resource):
imagebox1.image = properties.resources.test; unfortunately, seems image scaled reasons - cannot scroll it's border. if use 512x512 image, doesn't fit imagebox, seems cropped. ideas going on here?
using system; using system.drawing; using system.windows.forms; class imagebox : panel { public imagebox() { this.autoscroll = true; this.doublebuffered = true; } private image mimage; public image image { { return mimage; } set { mimage = value; if (mimage != null) this.autoscrollminsize = mimage.size; else this.autoscrollminsize = new size(0, 0); this.invalidate(); } } protected override void onpaint(painteventargs e) { e.graphics.translatetransform(this.autoscrollposition.x, this.autoscrollposition.y); if (mimage != null) e.graphics.drawimage(mimage, 0, 0); base.onpaint(e); } }
it issue resolution of image, less resolution of display. pretty unusual.
there more 1 workaround this. @taw's approach works favors monitor resolution. you'll sharper image not close image size recorded. other approach keep physical size, drawimage() does, , adjust scrollbars accordingly. change image property setter to:
set { mimage = value; if (value == null) this.autoscrollminsize = new size(0, 0); else { var size = value.size; using (var gr = this.creategraphics()) { size.width = (int)(size.width * gr.dpix / value.horizontalresolution); size.height = (int)(size.height * gr.dpiy / value.verticalresolution); } this.autoscrollminsize = size; } this.invalidate(); } picking "right" approach not obvious, ought consider adding property can change needed.
Comments
Post a Comment