每次我旋转我的图像时,我的系统内存不断增加
Every time i rotate my images, my system memory keeps going up
不确定我是不是疯了,但我有一个简单的滑块,可以旋转图像,但每次旋转图像时,系统内存都会增加 ,有没有办法清除这个缓存,我觉得我做错了什么。
public partial class Rulercompass : Form
{
Image img;
int angle;
public Rulercompass()
{
InitializeComponent();
}
private void Rulercompass_Load(object sender, EventArgs e)
{
img = Image.FromFile(@"C:\teste.jpg");
}
private void Rulercompass_Paint(object sender, PaintEventArgs e)
{
Bitmap bit_map = new Bitmap(img.Width, img.Height);
using(Graphics gfx = Graphics.FromImage(bit_map))
{
gfx.TranslateTransform(bit_map.Width / 2, bit_map.Height / 2);
gfx.RotateTransform(angle);
gfx.TranslateTransform(-bit_map.Width / 2, -bit_map.Height / 2);
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.DrawImage(img, 0, 0);
e.Graphics.TranslateTransform(this.Width / 2, this.Height / 2);
e.Graphics.DrawImage(bit_map, -bit_map.Width/2,-bit_map.Height/2);
}
}
private void Rulercompass_Resize(object sender, EventArgs e)
{
Invalidate();
}
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
angle = trackBar1.Value;
Invalidate();
}
}
}
找到解决方案,感谢 Ňɏssa Pøngjǣrdenlarp
private void Rulercompass_Paint(object sender, PaintEventArgs e)
{
Bitmap bit_map = new Bitmap(img.Width, img.Height);
using (Graphics gfx = Graphics.FromImage(bit_map))
{
gfx.TranslateTransform(bit_map.Width / 2, bit_map.Height / 2);
gfx.RotateTransform(angle);
gfx.TranslateTransform(-bit_map.Width / 2, -bit_map.Height / 2);
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.DrawImage(img, 0, 0);
e.Graphics.TranslateTransform(this.Width / 2, this.Height / 2);
e.Graphics.DrawImage(bit_map, -bit_map.Width / 2, -bit_map.Height / 2);
}
bit_map.Dispose();// after using dispose this is what was missing
}
不确定我是不是疯了,但我有一个简单的滑块,可以旋转图像,但每次旋转图像时,系统内存都会增加 ,有没有办法清除这个缓存,我觉得我做错了什么。
public partial class Rulercompass : Form
{
Image img;
int angle;
public Rulercompass()
{
InitializeComponent();
}
private void Rulercompass_Load(object sender, EventArgs e)
{
img = Image.FromFile(@"C:\teste.jpg");
}
private void Rulercompass_Paint(object sender, PaintEventArgs e)
{
Bitmap bit_map = new Bitmap(img.Width, img.Height);
using(Graphics gfx = Graphics.FromImage(bit_map))
{
gfx.TranslateTransform(bit_map.Width / 2, bit_map.Height / 2);
gfx.RotateTransform(angle);
gfx.TranslateTransform(-bit_map.Width / 2, -bit_map.Height / 2);
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.DrawImage(img, 0, 0);
e.Graphics.TranslateTransform(this.Width / 2, this.Height / 2);
e.Graphics.DrawImage(bit_map, -bit_map.Width/2,-bit_map.Height/2);
}
}
private void Rulercompass_Resize(object sender, EventArgs e)
{
Invalidate();
}
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
angle = trackBar1.Value;
Invalidate();
}
}
}
找到解决方案,感谢 Ňɏssa Pøngjǣrdenlarp
private void Rulercompass_Paint(object sender, PaintEventArgs e)
{
Bitmap bit_map = new Bitmap(img.Width, img.Height);
using (Graphics gfx = Graphics.FromImage(bit_map))
{
gfx.TranslateTransform(bit_map.Width / 2, bit_map.Height / 2);
gfx.RotateTransform(angle);
gfx.TranslateTransform(-bit_map.Width / 2, -bit_map.Height / 2);
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.DrawImage(img, 0, 0);
e.Graphics.TranslateTransform(this.Width / 2, this.Height / 2);
e.Graphics.DrawImage(bit_map, -bit_map.Width / 2, -bit_map.Height / 2);
}
bit_map.Dispose();// after using dispose this is what was missing
}