VB.NET/C#;使用BitBlt;在两者中使用相同的代码,VB.NET中会出现内存泄漏,但C中不会出现#
我希望在应用程序中使用BitBlt,在上找到了一个C#引用,并将其转换为VB.net。我主要使用VB.net,这就是我转换它的原因,并尝试使用它。它在C#中运行良好,但在VB.net中存在内存泄漏,我不确定如何修复它 代码: C#版本(从上面的源代码稍作修改)。 打开新项目,添加1个按钮和1个图片框,修改lstPics.add():VB.NET/C#;使用BitBlt;在两者中使用相同的代码,VB.NET中会出现内存泄漏,但C中不会出现#,c#,.net,vb.net,memory-leaks,bitblt,C#,.net,Vb.net,Memory Leaks,Bitblt,我希望在应用程序中使用BitBlt,在上找到了一个C#引用,并将其转换为VB.net。我主要使用VB.net,这就是我转换它的原因,并尝试使用它。它在C#中运行良好,但在VB.net中存在内存泄漏,我不确定如何修复它 代码: C#版本(从上面的源代码稍作修改)。 打开新项目,添加1个按钮和1个图片框,修改lstPics.add(): 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
[DllImport(“gdi32.dll”,EntryPoint=“SelectObject”)]
公共静态外部系统.IntPtr SelectObject(
[In()]System.IntPtr hdc,
[In()]System.IntPtr h);
[DllImport(“gdi32.dll”,EntryPoint=“DeleteObject”)]
[返回:Marshallas(UnmanagedType.Bool)]
公共静态外部布尔删除对象(
[In()]System.IntPtr-ho);
[DllImport(“gdi32.dll”,EntryPoint=“BitBlt”)]
[返回:Marshallas(UnmanagedType.Bool)]
公共静态外部布尔比特(
[In()]System.IntPtr hdc,int x,int y,int cx,int cy,
[In()]System.IntPtr hdcSrc、intx1、inty1、uint rop);
公共表格1()
{
初始化组件();
}
公共Int16 lstLoc=0;
私有无效按钮1\u单击(对象发送者,事件参数e)
{
List lstPics=新列表();
lstPics.Add(“C:\\1.jpg”);
lstPics.Add(“C:\\2.jpg”);
lstPics.Add(“C:\\3.jpg”);
如果((lstLoc==lstPics.Count))
{
lstLoc=0;
}
字符串strLoc=lstPics[lstLoc];
lstLoc++;
使用(位图bmp=(位图)Bitmap.FromFile(strLoc))
使用(Graphics grDest=Graphics.FromHwnd(pictureBox1.Handle))
使用(Graphics grSrc=Graphics.FromImage(bmp))
{
IntPtr hdcDest=IntPtr.Zero;
IntPtr hdcSrc=IntPtr.Zero;
IntPtr hBitmap=IntPtr.Zero;
IntPtr hOldObject=IntPtr.Zero;
尝试
{
hdcDest=grDest.GetHdc();
hdcSrc=grSrc.GetHdc();
hBitmap=bmp.GetHbitmap();
hOldObject=SelectObject(hdcsc、hBitmap);
if(hOldObject==IntPtr.Zero)
抛出新的Win32Exception();
如果(!BitBlt(hdcDest,0,0,pictureBox1.Width,pictureBox1.Height,
HDC,0,0,0x00CC0020U)
抛出新的Win32Exception();
}
最后
{
如果(hOldObject!=IntPtr.Zero)选择Object(hdcsc,hOldObject);
如果(hBitmap!=IntPtr.Zero)删除对象(hBitmap);
如果(hdcDest!=IntPtr.Zero)grDest.ReleaseHdc(hdcDest);
如果(hdcsc!=IntPtr.Zero)grSrc.ReleaseHdc(hdcsc);
}
}
}
}
}
VB.net版本(由我转换)。
打开新项目,添加1个按钮和1个图片框,修改lstPics.add():
导入System.ComponentModel
公开课表格1
将函数SelectObject Lib“gdi32.dll”别名“SelectObject”(ByVal hdc As System.IntPtr,ByVal h As System.IntPtr)公开声明为System.IntPtr
将函数DeleteObject Lib“gdi32.dll”别名“DeleteObject”(ByVal ho As System.IntPtr)公开声明为布尔值
将函数BitBlt Lib“gdi32.dll”的公共声明别名为“BitBlt”(ByVal hdc为System.IntPtr,ByVal x为Integer,ByVal y为Integer,ByVal cx为Integer,ByVal cy为Integer,ByVal hdcsc为System.IntPtr,ByVal x1为Integer,ByVal y1为Integer,ByVal rop为UInteger)作为布尔值
公共lstLoc作为Int16=0
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
尺寸mil1为Int64=0
将mil2调整为Int64=0
将mil3调整为Int64=0
Dim lstPics作为新列表(字符串)
添加(“C:\1.jpg”)
添加(“C:\2.jpg”)
添加(“C:\3.jpg”)
如果lstLoc=lstPics.Count,则lstLoc=0
Dim strLoc As String=lstPics(lstLoc)
lstLoc+=1
使用bmp作为Bitmap=Bitmap.FromFile(strLoc),
grDest As Graphics=Graphics.FromHwnd(PictureBox1.Handle),
grSrc As Graphics=Graphics.FromImage(bmp)
Dim hdcDest作为IntPtr=IntPtr.Zero
尺寸hdcSrc为IntPtr=IntPtr.Zero
尺寸hBitmap为IntPtr=IntPtr.Zero
Dim hOldObject作为IntPtr=IntPtr.Zero
尝试
hdcDest=grDest.GetHdc()
hdcSrc=grSrc.GetHdc()
hBitmap=bmp.GetHbitmap()
hOldObject=SelectObject(hdcsc、bmp.GetHbitmap)
如果是(hOldObject=IntPtr.Zero),则抛出新的Win32Exception()
如果不是BitBlt(hdcDest,0,0,PictureBox1.Width,PictureBox1.Height,hdcSrc,0,0,13369376),则抛出新的Win32Exception()
特例
MessageBox.Show(例如Message.ToString&vbNewLine&vbNewLine&ex.ToString)
最后
如果为(hOldObject IntPtr.Zero),则选择Object(hdcsc,hOldObject)
如果为(hBitmap IntPtr.Zero),则删除对象(hBitmap)
如果为(hdcDest IntPtr.Zero),则grDest.ReleaseHdc(hdcDest)
如果(HDCSC IntPtr.Zero),则grSrc.ReleaseHdc(HDCSC)
结束尝试
终端使用
端接头
末级
当你按下按钮并循环浏览不同的图片时,C#版本的内存使用率会急剧上升,但随后又会下降。V
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("gdi32.dll", EntryPoint = "SelectObject")]
public static extern System.IntPtr SelectObject(
[In()] System.IntPtr hdc,
[In()] System.IntPtr h);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject(
[In()] System.IntPtr ho);
[DllImport("gdi32.dll", EntryPoint = "BitBlt")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BitBlt(
[In()] System.IntPtr hdc, int x, int y, int cx, int cy,
[In()] System.IntPtr hdcSrc, int x1, int y1, uint rop);
public Form1()
{
InitializeComponent();
}
public Int16 lstLoc = 0;
private void button1_Click(object sender, EventArgs e)
{
List<string> lstPics = new List<string>();
lstPics.Add("C:\\1.jpg");
lstPics.Add("C:\\2.jpg");
lstPics.Add("C:\\3.jpg");
if ((lstLoc == lstPics.Count))
{
lstLoc = 0;
}
string strLoc = lstPics[lstLoc];
lstLoc++;
using (Bitmap bmp = (Bitmap)Bitmap.FromFile(strLoc))
using (Graphics grDest = Graphics.FromHwnd(pictureBox1.Handle))
using (Graphics grSrc = Graphics.FromImage(bmp))
{
IntPtr hdcDest = IntPtr.Zero;
IntPtr hdcSrc = IntPtr.Zero;
IntPtr hBitmap = IntPtr.Zero;
IntPtr hOldObject = IntPtr.Zero;
try
{
hdcDest = grDest.GetHdc();
hdcSrc = grSrc.GetHdc();
hBitmap = bmp.GetHbitmap();
hOldObject = SelectObject(hdcSrc, hBitmap);
if (hOldObject == IntPtr.Zero)
throw new Win32Exception();
if (!BitBlt(hdcDest, 0, 0, pictureBox1.Width, pictureBox1.Height,
hdcSrc, 0, 0, 0x00CC0020U))
throw new Win32Exception();
}
finally
{
if (hOldObject != IntPtr.Zero) SelectObject(hdcSrc, hOldObject);
if (hBitmap != IntPtr.Zero) DeleteObject(hBitmap);
if (hdcDest != IntPtr.Zero) grDest.ReleaseHdc(hdcDest);
if (hdcSrc != IntPtr.Zero) grSrc.ReleaseHdc(hdcSrc);
}
}
}
}
}
Imports System.ComponentModel
Public Class Form1
Public Declare Function SelectObject Lib "gdi32.dll" Alias "SelectObject" (ByVal hdc As System.IntPtr, ByVal h As System.IntPtr) As System.IntPtr
Public Declare Function DeleteObject Lib "gdi32.dll" Alias "DeleteObject" (ByVal ho As System.IntPtr) As Boolean
Public Declare Function BitBlt Lib "gdi32.dll" Alias "BitBlt" (ByVal hdc As System.IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal hdcSrc As System.IntPtr, ByVal x1 As Integer, ByVal y1 As Integer, ByVal rop As UInteger) As Boolean
Public lstLoc As Int16 = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim mil1 As Int64 = 0
Dim mil2 As Int64 = 0
Dim mil3 As Int64 = 0
Dim lstPics As New List(Of String)
lstPics.Add("C:\1.jpg")
lstPics.Add("C:\2.jpg")
lstPics.Add("C:\3.jpg")
If lstLoc = lstPics.Count Then lstLoc = 0
Dim strLoc As String = lstPics(lstLoc)
lstLoc += 1
Using bmp As Bitmap = Bitmap.FromFile(strLoc),
grDest As Graphics = Graphics.FromHwnd(PictureBox1.Handle),
grSrc As Graphics = Graphics.FromImage(bmp)
Dim hdcDest As IntPtr = IntPtr.Zero
Dim hdcSrc As IntPtr = IntPtr.Zero
Dim hBitmap As IntPtr = IntPtr.Zero
Dim hOldObject As IntPtr = IntPtr.Zero
Try
hdcDest = grDest.GetHdc()
hdcSrc = grSrc.GetHdc()
hBitmap = bmp.GetHbitmap()
hOldObject = SelectObject(hdcSrc, bmp.GetHbitmap)
If (hOldObject = IntPtr.Zero) Then Throw New Win32Exception()
If Not BitBlt(hdcDest, 0, 0, PictureBox1.Width, PictureBox1.Height, hdcSrc, 0, 0, 13369376) Then Throw New Win32Exception()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString & vbNewLine & vbNewLine & ex.ToString)
Finally
If (hOldObject <> IntPtr.Zero) Then SelectObject(hdcSrc, hOldObject)
If (hBitmap <> IntPtr.Zero) Then DeleteObject(hBitmap)
If (hdcDest <> IntPtr.Zero) Then grDest.ReleaseHdc(hdcDest)
If (hdcSrc <> IntPtr.Zero) Then grSrc.ReleaseHdc(hdcSrc)
End Try
End Using
End Sub
End Class