我必须在哪里写处理图像处理方法?
Where I have to write dispose in a image processing method?
代码在这里:
public Bitmap ScreenCaptureBitmap(int DesktopX, int DesktopY, int CaptureWidth, int CaptureHeight)
{
Bitmap ScreenCaptureBmp = new Bitmap(CaptureWidth, CaptureHeight);
Graphics graphics = Graphics.FromImage(ScreenCaptureBmp as Image);
graphics.CopyFromScreen(DesktopX, DesktopY, 0, 0, ScreenCaptureBmp.Size);
graphics.Dispose();
return ScreenCaptureBmp;
}
public Bitmap ResizeBitmap(Bitmap ResizeBmp, int RBmpWidth, int RBmpHeight)
{
Bitmap RBmp = new Bitmap(RBmpWidth, RBmpHeight);
using (Graphics RBmpG = Graphics.FromImage((Image)RBmp))
RBmpG.DrawImage(ResizeBmp, 0, 0, RBmpWidth, RBmpHeight);
return RBmp;
}
private void timer1_Tick(object sender, EventArgs e)
{
Bitmap Pic = ScreenCaptureBitmap(50, 50, 640, 320);
Bitmap Pic1 = ResizeBitmap(Pic, 128, 64);
pictureBox1.Image = Pic1;
}
我必须处理哪个位图? (我不知道我是否必须处理所有这些 "ScreenCaptureBmp"、"ResizeBmp"、"RBmp"、"Pic"、"Pic1" 或其中的一些)。
我必须处理方法的 return 位图吗? (示例:"ScreenCaptureBmp"、"RBmp")。
我在这段代码中是否以正确的方式处理了 Graphics("graphics", "RBmpG")?
我必须在哪里写处置才能以正确的方式处置?
如果我写这段代码:
public Bitmap ScreenCaptureBitmap(int DesktopX, int DesktopY, int CaptureWidth, int CaptureHeight)
{
Bitmap ScreenCaptureBmp = new Bitmap(CaptureWidth, CaptureHeight);
using (Graphics graphics = Graphics.FromImage(ScreenCaptureBmp as Image))
graphics.CopyFromScreen(DesktopX, DesktopY, 0, 0, ScreenCaptureBmp.Size);
return ScreenCaptureBmp;
}
public Bitmap ResizeBitmap(Bitmap ResizeBmp, int RBmpWidth, int RBmpHeight)
{
Bitmap RBmp = new Bitmap(RBmpWidth, RBmpHeight);
using (Graphics RBmpG = Graphics.FromImage((Image)RBmp))
RBmpG.DrawImage(ResizeBmp, 0, 0, RBmpWidth, RBmpHeight);
return RBmp;
}
private void timer1_Tick(object sender, EventArgs e)
{
Bitmap Pic = ScreenCaptureBitmap(50, 50, 640, 320);
Bitmap Pic1 = ResizeBitmap(Pic, 128, 64);
Pic.Dispose();
using (Image PreviewImage = pictureBox1.Image)
{
pictureBox1.Image = Pic1;
}
}
这(第 2 段)代码中的所有内容是否都以正确的方式处理?
此(第 2 段)代码中 "timer1_Tick" 方法的配置是否正确?
这适用于(或至少应该适用于)所有语言的所有事物:
Dispose 是在您使用完某些东西时调用,或者如果一次性对象不会超过它声明的范围。(参见:Proper use of the IDisposable interface)
在 C# 中,使用 using
块会在完成执行时自动处理其中的所有内容。 (参见:Uses of "using" in C#)
在你的情况下,在你的 ScreenCaptureBitmap
方法中,你的 Graphics
对象被正确处理,因为
- 调用完成后
CopyFromScreen
- 不会在
ScreenCaptureBitmap
方法之外继续活下去
在你的 ResizeBitmap
方法中,你没有释放 RBmp
因为你正在返回它,它会继续存在于方法之外。
在你的 ResizeBitmap
方法中,你会自动处理你的 RBmpG
因为它在 using
块中。
在您的 timer1_Tick
方法中,在您使用 Pic
帮助创建 Pic1
后,您应该将其丢弃,因为您不再使用它并且不需要活过方法的范围。
在您的 timer1_Tick
方法中,您没有处置 Pic1
,因为它已分配给 pictureBox1.Image,并且 pictureBox1 存在于该方法的范围之外。
但是,由于它是一个 tick
方法,我假设它会被重复调用,并且你应该清理(处理)pictureBox1
的前一个 Image
,前提是它存在。
Did I have to dispose return Bitmap of a method? (Example: "ScreenCaptureBmp", "RBmp").
不,你没有。您可以使用 API 方法 (ScreenCaptureBitmap/ResizeBitmap) 的代码。
Did I dispose Graphics("graphics", "RBmpG") in right way in this code?
是的,演示的方法是正确的,但我个人更喜欢使用 using
方法,因为它更可靠(您可以 google 更多地了解 using
的好处。简而言之,它保证了 Dispose 调用并封装了所有需要的检查):
public Bitmap ScreenCaptureBitmap(int DesktopX, int DesktopY, int CaptureWidth, int CaptureHeight)
{
Bitmap ScreenCaptureBmp = new Bitmap(CaptureWidth, CaptureHeight);
using(var graphics = Graphics.FromImage(ScreenCaptureBmp))
graphics.CopyFromScreen(DesktopX, DesktopY, 0, 0, ScreenCaptureBmp.Size);
return ScreenCaptureBmp;
}
但是您对这种 API 用法不满意。正确的版本应该是这样的:
void timer1_Tick(object sender, EventArgs e)
{
using(Bitmap Pic = ScreenCaptureBitmap(50, 50, 640, 320)) {
Image oldImage = pictureBox1.Image;
using(oldImage)
pictureBox1.Image = ResizeBitmap(Pic, 128, 64);
}
}
代码在这里:
public Bitmap ScreenCaptureBitmap(int DesktopX, int DesktopY, int CaptureWidth, int CaptureHeight)
{
Bitmap ScreenCaptureBmp = new Bitmap(CaptureWidth, CaptureHeight);
Graphics graphics = Graphics.FromImage(ScreenCaptureBmp as Image);
graphics.CopyFromScreen(DesktopX, DesktopY, 0, 0, ScreenCaptureBmp.Size);
graphics.Dispose();
return ScreenCaptureBmp;
}
public Bitmap ResizeBitmap(Bitmap ResizeBmp, int RBmpWidth, int RBmpHeight)
{
Bitmap RBmp = new Bitmap(RBmpWidth, RBmpHeight);
using (Graphics RBmpG = Graphics.FromImage((Image)RBmp))
RBmpG.DrawImage(ResizeBmp, 0, 0, RBmpWidth, RBmpHeight);
return RBmp;
}
private void timer1_Tick(object sender, EventArgs e)
{
Bitmap Pic = ScreenCaptureBitmap(50, 50, 640, 320);
Bitmap Pic1 = ResizeBitmap(Pic, 128, 64);
pictureBox1.Image = Pic1;
}
我必须处理哪个位图? (我不知道我是否必须处理所有这些 "ScreenCaptureBmp"、"ResizeBmp"、"RBmp"、"Pic"、"Pic1" 或其中的一些)。
我必须处理方法的 return 位图吗? (示例:"ScreenCaptureBmp"、"RBmp")。
我在这段代码中是否以正确的方式处理了 Graphics("graphics", "RBmpG")?
我必须在哪里写处置才能以正确的方式处置?
如果我写这段代码:
public Bitmap ScreenCaptureBitmap(int DesktopX, int DesktopY, int CaptureWidth, int CaptureHeight)
{
Bitmap ScreenCaptureBmp = new Bitmap(CaptureWidth, CaptureHeight);
using (Graphics graphics = Graphics.FromImage(ScreenCaptureBmp as Image))
graphics.CopyFromScreen(DesktopX, DesktopY, 0, 0, ScreenCaptureBmp.Size);
return ScreenCaptureBmp;
}
public Bitmap ResizeBitmap(Bitmap ResizeBmp, int RBmpWidth, int RBmpHeight)
{
Bitmap RBmp = new Bitmap(RBmpWidth, RBmpHeight);
using (Graphics RBmpG = Graphics.FromImage((Image)RBmp))
RBmpG.DrawImage(ResizeBmp, 0, 0, RBmpWidth, RBmpHeight);
return RBmp;
}
private void timer1_Tick(object sender, EventArgs e)
{
Bitmap Pic = ScreenCaptureBitmap(50, 50, 640, 320);
Bitmap Pic1 = ResizeBitmap(Pic, 128, 64);
Pic.Dispose();
using (Image PreviewImage = pictureBox1.Image)
{
pictureBox1.Image = Pic1;
}
}
这(第 2 段)代码中的所有内容是否都以正确的方式处理?
此(第 2 段)代码中 "timer1_Tick" 方法的配置是否正确?
这适用于(或至少应该适用于)所有语言的所有事物:
Dispose 是在您使用完某些东西时调用,或者如果一次性对象不会超过它声明的范围。(参见:Proper use of the IDisposable interface)
在 C# 中,使用 using
块会在完成执行时自动处理其中的所有内容。 (参见:Uses of "using" in C#)
在你的情况下,在你的 ScreenCaptureBitmap
方法中,你的 Graphics
对象被正确处理,因为
- 调用完成后
CopyFromScreen
- 不会在
ScreenCaptureBitmap
方法之外继续活下去
在你的 ResizeBitmap
方法中,你没有释放 RBmp
因为你正在返回它,它会继续存在于方法之外。
在你的 ResizeBitmap
方法中,你会自动处理你的 RBmpG
因为它在 using
块中。
在您的 timer1_Tick
方法中,在您使用 Pic
帮助创建 Pic1
后,您应该将其丢弃,因为您不再使用它并且不需要活过方法的范围。
在您的 timer1_Tick
方法中,您没有处置 Pic1
,因为它已分配给 pictureBox1.Image,并且 pictureBox1 存在于该方法的范围之外。
但是,由于它是一个 tick
方法,我假设它会被重复调用,并且你应该清理(处理)pictureBox1
的前一个 Image
,前提是它存在。
Did I have to dispose return Bitmap of a method? (Example: "ScreenCaptureBmp", "RBmp").
不,你没有。您可以使用 API 方法 (ScreenCaptureBitmap/ResizeBitmap) 的代码。
Did I dispose Graphics("graphics", "RBmpG") in right way in this code?
是的,演示的方法是正确的,但我个人更喜欢使用 using
方法,因为它更可靠(您可以 google 更多地了解 using
的好处。简而言之,它保证了 Dispose 调用并封装了所有需要的检查):
public Bitmap ScreenCaptureBitmap(int DesktopX, int DesktopY, int CaptureWidth, int CaptureHeight)
{
Bitmap ScreenCaptureBmp = new Bitmap(CaptureWidth, CaptureHeight);
using(var graphics = Graphics.FromImage(ScreenCaptureBmp))
graphics.CopyFromScreen(DesktopX, DesktopY, 0, 0, ScreenCaptureBmp.Size);
return ScreenCaptureBmp;
}
但是您对这种 API 用法不满意。正确的版本应该是这样的:
void timer1_Tick(object sender, EventArgs e)
{
using(Bitmap Pic = ScreenCaptureBitmap(50, 50, 640, 320)) {
Image oldImage = pictureBox1.Image;
using(oldImage)
pictureBox1.Image = ResizeBitmap(Pic, 128, 64);
}
}