c# - How do I draw the image to the pictureBox? -


in top of form1, have:

bitmap bitmap; 

in constructor:

bitmap = (bitmap)sc.capturescreen(); 

create 100 screenshots:

for (int = 0; < 100; i++) {     bitmap.save(@"d:\ffmpegtorun\ffmpeg-20130509-git-13cb6ed-win32-static\bin\screenshots\" + "screenshot" + i.tostring("d6") + ".jpg", imageformat.jpeg); } 

put screenshot picturebox1:

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

then in picturebox1 paint event i'm doing:

private void picturebox1_paint(object sender, painteventargs e) {     int width = picturebox1.width;     int height = picturebox1.height;      // draw image no shrinking or stretching.     e.graphics.interpolationmode = interpolationmode.highqualitybicubic;     e.graphics.drawimage(                 bitmap,                 new rectangle(0, 0, width, height),  // destination rectangle                   0,                 0,           // upper-left corner of source rectangle                 width,       // width of source rectangle                 height,      // height of source rectangle                 graphicsunit.pixel,                 null);  } 

and screenshot class it's taking screenshots:

using system; using system.runtime.interopservices; using system.drawing; using system.drawing.imaging; namespace screenshotdemo {     /// <summary>     /// provides functions capture entire screen, or particular window, , save file.     /// </summary>     public class screencapture     {         /// <summary>         /// creates image object containing screen shot of entire desktop         /// </summary>         /// <returns></returns>         public image capturescreen()         {             return capturewindow(user32.getdesktopwindow());         }         /// <summary>         /// creates image object containing screen shot of specific window         /// </summary>         /// <param name="handle">the handle window. (in windows forms, obtained handle property)</param>         /// <returns></returns>         public image capturewindow(intptr handle)         {             // te hdc of target window             intptr hdcsrc = user32.getwindowdc(handle);             // size             user32.rect windowrect = new user32.rect();             user32.getwindowrect(handle, ref windowrect);             int width = windowrect.right - windowrect.left;             int height = windowrect.bottom - windowrect.top;             // create device context can copy             intptr hdcdest = gdi32.createcompatibledc(hdcsrc);             // create bitmap can copy to,             // using getdevicecaps width/height             intptr hbitmap = gdi32.createcompatiblebitmap(hdcsrc, width, height);             // select bitmap object             intptr hold = gdi32.selectobject(hdcdest, hbitmap);             // bitblt on             gdi32.bitblt(hdcdest, 0, 0, width, height, hdcsrc, 0, 0, gdi32.srccopy);             // restore selection             gdi32.selectobject(hdcdest, hold);             // clean              gdi32.deletedc(hdcdest);             user32.releasedc(handle, hdcsrc);             // .net image object             image img = image.fromhbitmap(hbitmap);             // free bitmap object             gdi32.deleteobject(hbitmap);             return img;         }         /// <summary>         /// captures screen shot of specific window, , saves file         /// </summary>         /// <param name="handle"></param>         /// <param name="filename"></param>         /// <param name="format"></param>         public void capturewindowtofile(intptr handle, string filename, imageformat format)         {             image img = capturewindow(handle);             img.save(filename, format);         }         /// <summary>         /// captures screen shot of entire desktop, , saves file         /// </summary>         /// <param name="filename"></param>         /// <param name="format"></param>         public void capturescreentofile(string filename, imageformat format)         {             image img = capturescreen();             img.save(filename, format);         }          /// <summary>         /// helper class containing gdi32 api functions         /// </summary>         private class gdi32         {              public const int srccopy = 0x00cc0020; // bitblt dwrop parameter             [dllimport("gdi32.dll")]             public static extern bool bitblt(intptr hobject, int nxdest, int nydest,                 int nwidth, int nheight, intptr hobjectsource,                 int nxsrc, int nysrc, int dwrop);             [dllimport("gdi32.dll")]             public static extern intptr createcompatiblebitmap(intptr hdc, int nwidth,                 int nheight);             [dllimport("gdi32.dll")]             public static extern intptr createcompatibledc(intptr hdc);             [dllimport("gdi32.dll")]             public static extern bool deletedc(intptr hdc);             [dllimport("gdi32.dll")]             public static extern bool deleteobject(intptr hobject);             [dllimport("gdi32.dll")]             public static extern intptr selectobject(intptr hdc, intptr hobject);         }          /// <summary>         /// helper class containing user32 api functions         /// </summary>         private class user32         {             [structlayout(layoutkind.sequential)]             public struct rect             {                 public int left;                 public int top;                 public int right;                 public int bottom;             }             [dllimport("user32.dll")]             public static extern intptr getdesktopwindow();             [dllimport("user32.dll")]             public static extern intptr getwindowdc(intptr hwnd);             [dllimport("user32.dll")]             public static extern intptr releasedc(intptr hwnd, intptr hdc);             [dllimport("user32.dll")]             public static extern intptr getwindowrect(intptr hwnd, ref rect rect);         }     } } 

the result this:

https://skydrive.live.com/redir?resid=eb1c71c44c3976d5!270&authkey=!anakzbwji-3tqqe

the image not fit picturebox.

but if i'm moving magnifier glass on picturebox see smooth image not fit picturebox:

https://skydrive.live.com/redir?resid=eb1c71c44c3976d5!271&authkey=!alizvsyxrmuyvtk

this magnifier glass form code:

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;             }         }     } } 

you can set e.graphics.interpolationmode interpolationmode.highqualitybicubic.
use line:

e.graphics.interpolationmode = interpolationmode.highqualitybicubic; 

before drawing image. smoothes image.

don't forget:

using system.drawing.drawing2d; 

edit:
instead of using this:

int width = bitmap.width; int height = bitmap.height; 

use that:

int width = picturebox1.width; int height = picturebox1.height; 

to resize drawn bitmap size of picturebox.

edit #2:
have use different overload of drawimage method:

e.graphics.drawimage(bitmap, new rectangle(0, 0, width, height)); 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -