Home > Net >  Bitmap. GetHicon () method of how to release the memory
Bitmap. GetHicon () method of how to release the memory

Time:09-25

I wrote a winform screen magnifier tool in c #, centering on the mouse position, take a picture, will figure Cursor images, real-time amplification, this method through the timer tick event constantly call, I found the Cursor=new Cursor (bitmap. The GetHicon ()); This line of code that will increase the physical memory footprint, lead to system gradually slow down, please everyone a great god is there any solution,

CodePudding user response:

You keep the Cursor=new Cursor (bitmap GetHicon ());
So where is the release before the new Cursor (bitmap. GetHicon ()) of?

CodePudding user response:

New object is created, of course, the memory will increase

CodePudding user response:

reference 1st floor xuzuning response:
you constantly Cursor=new Cursor (bitmap. GetHicon ());
So where is the release before the new Cursor (bitmap. GetHicon ()) of?


That should be how to release?

CodePudding user response:

I don't know how to do a magnifying glass and cursor associated

CodePudding user response:

Attach relevant code
 
Private void RefreshImg ()
{
Var mousePos=mousePos;//mouse position
Var updatedBox=GetUpdatedBox (mousePos);//according to the mouse position draw a rectangle
Var img=GetDesktopImg (updatedBox);//to get related screen screenshots

SetCursor (img);//convert the screenshot to the cursor

MousePos=new Point ();
UpdatedBox=new Rectangle ();
Img=null;
}

Private Rectangle GetUpdatedBox (Point p)
{
Return a Rectangle. Truncate (new RectangleF (p.X - magnifierWidth/zoomValue/2, p.Y - magnifierHeight zoomValue/2, magnifierWidth/zoomValue, magnifierHeight/zoomValue));
}

Private Bitmap GetDesktopImg (a Rectangle the rect)
{
IntPtr hWnd=GetDesktopWindow ();
System, Drawing Graphics grpScreen=System. Drawing, Graphics. FromHwnd (hWnd);

System. Drawing. The Bitmap Bitmap=new System. Drawing. The Bitmap (the rect. Width, the rect. Height, System. Drawing. The Imaging. PixelFormat. Format32bppArgb);
System, Drawing Graphics grpBitmap=System. Drawing, Graphics. FromImage (bitmap);
IntPtr hdcScreen=grpScreen. GetHdc ();
IntPtr hdcBitmap=grpBitmap. GetHdc ();
BitBlt (hdcBitmap, 0, 0, bitmap. The Width, the bitmap. The Height, hdcScreen, the rect. Left, the rect. Top, 0 x00cc0020);
GrpBitmap. ReleaseHdc (hdcBitmap);
GrpScreen. ReleaseHdc (hdcScreen);
GrpBitmap. The Dispose ();
GrpScreen. The Dispose ();
return bitmap;
}

Private void SetCursor (Bitmap img)
{
Using (var g=Graphics. FromImage (magnifierImg))
{
G.D rawImage (img, 0, 0, magnifierImg Width, magnifierImg, Height);
G.D rawRectangle (new Pen (new SolidBrush (Color. Black)), the new Rectangle (0, 0, magnifierImg. Width - 1, magnifierImg. Height - 1));
G.D ispose ();
}

Cursor=new Cursor (magnifierImg GetHicon ());//how to release?
}

CodePudding user response:

reference 4 floor crystal_lz response:
I don't know how to do a magnifying glass and cursor between the

To enlarge the image to make the mouse cursor

CodePudding user response:

Only a magnifying glass which have so trouble and do not see your code where there is the enlarge images

 
Public partial class Form6: Form
{
Public Form6 () {
InitializeComponent();
This. The timer1. Interval=50;
Enclosing FormBorderStyle=FormBorderStyle. None;
}

Private Point m_ptLast;
Private Size m_szSrc;
Private float m_fScale;
Private Bitmap m_bmpSrc;

Private void Form6_Load (object sender, EventArgs e) {
M_fScale=4;
M_szSrc=https://bbs.csdn.net/topics/new System. Drawing. The Size (15, 15);
M_bmpSrc=https://bbs.csdn.net/topics/new Bitmap (m_szSrc. Width, m_szSrc. Height);
Timer1. Enabled=true;
}

Private void timer1_Tick (object sender, EventArgs e) {
This. TopMost=true;//is something wrong with my system? TopMost attributes was invalid can only set the Timer inside kept calling to effective helpless
If (m_ptLast==MousePosition) return;
M_ptLast=MousePosition;
Using (Graphics g=Graphics. FromImage (m_bmpSrc)) {
Right opyFromScreen (MousePosition. X-ray m_bmpSrc. Width/2, MousePosition. Y - m_bmpSrc. Height/2, 0, 0, m_bmpSrc. Size);
G.D rawRectangle (Pens. The Magenta, 0, 0, m_bmpSrc. Width 1, m_bmpSrc. Height - 1);
Enclosing the BackgroundImage=ZoomImage (m_bmpSrc m_fScale);
This. Size=this. BackgroundImage. Size;
This. Left=MousePosition. X + 20;//according to the mouse Settings window position
This. Top=MousePosition. Y + 20;//there is no judge whether form beyond the edge of the screen
}
}

Public static Image ZoomImage (Image, Image, float scale) {
If (image==null)
Throw new ArgumentNullException (" image always be null ");
If (scale & lt;=0)
Throw new ArgumentException (" scale must be wining zero ");
Bitmap BMP=new Bitmap ((int) Math. Between (image. Width * scale), (int) Math. Between (image. * scale Height));
As Bitmap Bitmap bmpOld=image. Clone ();
BitmapData bmpDataNew=BMP. LockBits (new Rectangle (Point. The Empty, BMP. Size), ImageLockMode. WriteOnly, PixelFormat. Format32bppArgb);
BitmapData bmpDataOld=bmpOld. LockBits (new Rectangle (Point. The Empty, image Size), ImageLockMode. ReadOnly, PixelFormat. Format32bppArgb);
Byte [] byColorNew=new byte [bmpDataNew Height * bmpDataNew in Stride];
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related