c# - How can i change the Image to be in the same size of the pictureBox? -


i have function:

public static bitmap resizeimage(bitmap imgtoresize, size size)         {             try             {                 bitmap b = new bitmap(size.width, size.height);                 using (graphics g = graphics.fromimage((image)b))                 {                     g.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;                      g.drawimage(imgtoresize, 0, 0, size.width, size.height);                 }                  return b;             }             catch             {                 throw;             }         } 

but how use put in size size ? tried call function like: resizeimage(bitmap,new size(100,100)); that;s not way.

in form1 did:

bitmap = new bitmap(@"d:\ffmpegtorun\ffmpeg-20130509-git-13cb6ed-win32-static\bin\screenshots\screenshot000000.jpg");             picturebox1.image = bitmap; 

since blurry inside picturebox if it's zoom mode or stretchimage mode thought resize image size of picturebox , maybe change picturebox mode normal ot stretchimage ? i'm not sure how fix blurry in picturebox.

i'm adding screenshot here of magnifier glass working show blurry when moving on picturebox.

this when it's in stretchimage mode it's same when it's set zoom mode:

https://skydrive.live.com/redir?resid=eb1c71c44c3976d5!269&authkey=!ahi8lmsnzhfka60

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; using system.drawing.drawing2d; using system.io; using system.drawing.imaging;  namespace magnifier20070401 {     public partial class magnifierform : form     {         private bool _domove;          public magnifierform()         {             initializecomponent();              _domove = true;             formborderstyle = formborderstyle.none;             showintaskbar = false;             topmost = true;             width = 150;             height = 150;             graphicspath gp = new graphicspath();             gp.addellipse(clientrectangle);             region = new region(gp);             mtimer = new timer();             mtimer.enabled = true;             mtimer.interval = 20;             mtimer.tick += new eventhandler(handletimer);             mscreenimage = new bitmap(screen.primaryscreen.bounds.width,                                      screen.primaryscreen.bounds.height);             mstartpoint = new point(500, 500);             mtargetpoint = new point(500, 500);             speed = 0.35f;             zoom = 3.0f;             hidecursor = false;             hue = 30;         }          public magnifierform(bool movetheglass, bool intaskbar, int magnifierwidth, int magnifierheight, point magnifierstartpoint, float speedfactor, float changezoom, bool hidemousecursor, float adjusthue)         {             initializecomponent();              formborderstyle = formborderstyle.none;             topmost = true;             graphicspath gp = new graphicspath();             gp.addellipse(clientrectangle);             region = new region(gp);             mtimer = new timer();             mtimer.enabled = true;             mtimer.interval = 20;             mtimer.tick += new eventhandler(handletimer);             mscreenimage = new bitmap(screen.primaryscreen.bounds.width,                                      screen.primaryscreen.bounds.height);              _domove = movetheglass;             showintaskbar = intaskbar;             width = magnifierwidth;             height = magnifierheight;             mstartpoint = magnifierstartpoint;             mtargetpoint = magnifierstartpoint;             speed = speedfactor;             zoom = changezoom;             hidecursor = hidemousecursor;             hue = adjusthue;         }          protected override void onshown(eventargs e)         {             repositionandshow();         }          private delegate void repositionandshowdelegate();          private void repositionandshow()         {             if (invokerequired)             {                 invoke(new repositionandshowdelegate(repositionandshow));             }             else             {                 graphics g = graphics.fromimage(mscreenimage);                 g.copyfromscreen(0, 0, 0, 0, new size(mscreenimage.width, mscreenimage.height));                 hsladjust.bitmapfunctions bf = new hsladjust.bitmapfunctions((bitmap)mscreenimage);                 bf.hue(hue);                 bf.dispose();                 g.dispose();                  if (hidecursor == true)                 {                     cursor.hide();                 }                 else                 {                     cursor = cursors.cross;                 }                 capture = true;                 mcurrentpoint = cursor.position;                 show(); // add here bool of mouse cursor hide or not hide             }         }          void handletimer(object sender, eventargs e)         {             float dx = speed * (mtargetpoint.x - mcurrentpoint.x);             float dy = speed * (mtargetpoint.y - mcurrentpoint.y);              if (mfirsttime)             {                 mfirsttime = false;                  mcurrentpoint.x = mtargetpoint.x;                 mcurrentpoint.y = mtargetpoint.y;                  left = (int)mcurrentpoint.x - width / 2;                 top = (int)mcurrentpoint.y - height / 2;                  return;             }              mcurrentpoint.x += dx;             mcurrentpoint.y += dy;              if (math.abs(dx) < 1 && math.abs(dy) < 1)             {                 mtimer.enabled = false;             }             else             {                 // update location                 left = (int)mcurrentpoint.x - width / 2;                 top = (int)mcurrentpoint.y - height / 2;                 mlastmagnifierposition = new point((int)mcurrentpoint.x, (int)mcurrentpoint.y);             }              refresh();         }           protected override void onmousedown(mouseeventargs e)         {             moffset = new point(width / 2 - e.x, height / 2 - e.y);             mcurrentpoint = pointtoscreen(new point(e.x + moffset.x, e.y + moffset.y));             mtargetpoint = mcurrentpoint;             mtimer.enabled = true;          }          protected override void onmousemove(mouseeventargs e)         {             if (_domove == true)             {                 mtargetpoint = pointtoscreen(new point(e.x + moffset.x, e.y + moffset.y));                 mtimer.enabled = true;             }          }          protected override void onpaintbackground(painteventargs e)         {             /*if (mconfiguration.doublebuffered)             {                 // not paint background (required double buffering)!             }             else             {                 base.onpaintbackground(e);             }*/             base.onpaintbackground(e);         }          protected override void  onpaint(painteventargs e)         {             if (mbufferimage == null)             {                 mbufferimage = new bitmap(width, height);             }             graphics buffergrf = graphics.fromimage(mbufferimage);              graphics g;              /*if (mconfiguration.doublebuffered)             {                 g = buffergrf;             }             else             {*/                 g = e.graphics;             //}              if (mscreenimage != null)             {                 rectangle dest = new rectangle(0, 0, width, height);                 int w = (int)(width / zoom);//mconfiguration.zoomfactor);                 int h = (int)(height / zoom);//mconfiguration.zoomfactor);                 int x = left - w / 2 + width / 2;                 int y = top - h / 2 + height / 2;                  g.drawimage(                     mscreenimage,                     dest,                     x, y,                     w, h,                     graphicsunit.pixel);             }                 e.graphics.drawimage(mbufferimage, 0, 0, width, height);               }           //--- data members ---         #region data members         private bool hidecursor;         private float zoom;         private float hue;         private float speed;         private timer mtimer;         private image mbufferimage = null;         private image mscreenimage = null;         private point mstartpoint;         private pointf mtargetpoint;         private pointf mcurrentpoint;         private point moffset;         private bool mfirsttime = true;         private static point mlastmagnifierposition = cursor.position;         #endregion           // new code \\          protected override void onmouseenter(eventargs e)         {             base.onmouseenter(e);              point pt = control.mouseposition;             int ex = pt.x - this.left;             int ey = pt.y - this.top;              moffset = new point(0, 0);             mcurrentpoint = pointtoscreen(new point(ex + moffset.x, ey + moffset.y));             mtargetpoint = mcurrentpoint;             mtimer.enabled = true;             this.capture = true;         }          protected override void onmouseleave(eventargs e)         {             base.onmouseleave(e);              if (_domove)             {                 left = (int)mcurrentpoint.x - width / 2;                 top = (int)mcurrentpoint.y - height / 2;             }         }          protected override void onmouseclick(mouseeventargs e)         {             base.onmouseclick(e);             if (_domove == true)             {                 _domove = false;             }             else             {                 _domove = true;             }         }     } } 

this magnifier glass form code maybe can test , see how blurry when moving on picturebox.

this how turn on/off magnifier glass in form1.

protected override bool processcmdkey(ref message msg, keys keydata)         {             if (keydata == (keys.control | keys.m))             {                 if (mf == null)                 {                     mf = new magnifier20070401.magnifierform();                     mf.startposition = formstartposition.manual;                     mf.location = control.mouseposition;                     mf.show();                      this.select();                 }                 else if (mf.isdisposed)                 {                     mf = new magnifier20070401.magnifierform();                     mf.startposition = formstartposition.manual;                     mf.location = control.mouseposition;                     mf.show();                 }                 else                 {                     mf.close();                     mf = null;                 }             }             return base.processcmdkey(ref msg, keydata);         } 

set picturebox sizemode zoom

picturebox1.sizemode = pictureboxsizemode.zoom;


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -