static WeakReference wr = new WeakReference(null);
private void SetTheCsOnlineSight()
{
try
{
if (!isReult)
{
return;
}
IntPtr hscrdc = GetWindowDC(csHwnd);//调用api
Rectangle rect = new Rectangle();
GetClientRect(csHwnd, ref rect);//调用api获得窗口信息
Graphics g = wr.Target as Graphics; //Graphics.FromHdc(hscrdc);
int height = 0;
if (g == null)
{
g = Graphics.FromHdc(hscrdc);
wr.Target = g;
}
if (rdWindow.Checked)
{
//窗口化时需要加上任务栏高度
height = rect.Height / 2 + (Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height) + u;
}
if (rdAllScreen.Checked)
{
height = rect.Height / 2 + u;
}
g.DrawEllipse(new Pen(color), rect.Width / 2 + l, height, w, h);//准星大小
g.FillEllipse(new SolidBrush(color), (rect.Right - rect.Left) / 2 + l, height, fw, fh);//填充为实型
}
catch { }
finally
{
GC.Collect();
}
}改成弱引用还是会内存溢出,求高手解答,在线等.
private void SetTheCsOnlineSight()
{
try
{
if (!isReult)
{
return;
}
IntPtr hscrdc = GetWindowDC(csHwnd);//调用api
Rectangle rect = new Rectangle();
GetClientRect(csHwnd, ref rect);//调用api获得窗口信息
Graphics g = wr.Target as Graphics; //Graphics.FromHdc(hscrdc);
int height = 0;
if (g == null)
{
g = Graphics.FromHdc(hscrdc);
wr.Target = g;
}
if (rdWindow.Checked)
{
//窗口化时需要加上任务栏高度
height = rect.Height / 2 + (Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height) + u;
}
if (rdAllScreen.Checked)
{
height = rect.Height / 2 + u;
}
g.DrawEllipse(new Pen(color), rect.Width / 2 + l, height, w, h);//准星大小
g.FillEllipse(new SolidBrush(color), (rect.Right - rect.Left) / 2 + l, height, fw, fh);//填充为实型
}
catch { }
finally
{
GC.Collect();
}
}改成弱引用还是会内存溢出,求高手解答,在线等.
marsh.free(hscrdc );
graphics g=this.creategraphics();就可以了。
只是那个画圆的,内存一直增长,最后就内存溢出
EndHandle := GetCurrentProcess();
if GetProcessWorkingSetSize(EndHandle, currentMinWorkingSetValue, currentMaxWorkingSetValue) then
begin
SetProcessWorkingSetSize(EndHandle, currentMinWorkingSetValue, currentMaxWorkingSetValue);
end;
SetProcessWorkingSetSize()
把这两个函数在c#里定义了,然后调用看看。另外,如果你是在别的窗口上画图,一定要找到别的程序的句柄,
EndHandle := GetCurrentProcess();
就是这句话,必须是别的程序的进程句柄。
你现在光释放你程序的资源,所以会出错