帮我优化我的EnhancedPictureBox [英] Help me to optimize my EnhancedPictureBox
问题描述
大家好!
我创建了一个从PictureBox派生的新类以节省堆内存
使用PictureBox.Image或PictureBox.ImageLocation属性时的原因
它将图像加载到堆内存中,并且工作集有时会跳转
甚至达到120 MB(在图片之间导航并创建新的位图时
对象)
相反,我使用Graphics类
直接在PictureBox.ClientRectangle表面绘制图像.
代码:
Hi everyone!
I''ve created a new class derived from PictureBox to save heap memory
cause when using PictureBox.Image or PictureBox.ImageLocation properties
it loads the images into heap memory and the Working Set sometimes jumps
even to 120 MB (when navigating among pictures and creating new Bitmap
objects)
Instead I draw the images directly at the PictureBox.ClientRectangle surface using Graphics class
Code:
public class EnhancedPictureBox: PictureBox
{
public EnhancedPictureBox()
{
this.Image = null;
// --- I don''t use Image property ---
}
// --- path to an image file to draw ---
private string path = null;
public string PathToDraw
{
get { return this.path; }
set
{
this.path = value;
if (PathToDraw != null)
Draw(); // --- if an image exists ---
}
}
private void Draw()
{
using(Stream stream = new FileStream(PathToDraw, FileMode.Open))
{
using(Image image = Image.FromStream(stream))
{
using( Graphics grfx = this.CreateGraphics())
{
grfx.DrawImage(image, 0,0, this.Width, this.Height);
}
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
Draw();
}
}
现在我的程序只吃了30 MB !!!!!!
我有一些问题:
首先,在加载文件时使用流有效....
我正在寻找最佳解决方案,因此我的课程不会装载任何东西来堆,而是从硬盘驱动器中读取所有内容...
我一直在搜索MSDN,但发现有
System.IO命名空间中的UnmanagedMemoryStream ...
如何使用此流直接使用它来引用硬盘上的映像...它使用了一些IntPtr指针...那是什么?我知道它运行非托管代码,但性能更好...
.NET Framework中是否有任何专门的图像读取流?
请帮忙!
[edit]添加了代码块,以整理格式. OriginalGriff [/edit]
Now my program eats only 30 MB!!!!!!!!
I have some questions:
First, is ot effective to use streams when loading files ....
I''m searching for the best solution so my class would not load anything to heap, but read everything from a hard drive ...
I''ve been searching MSDN and I found that there is
an UnmanagedMemoryStream in System.IO namespace...
How to use it to reference directly to the image on the hard drive with this stream ... It uses some IntPtr pointers... what is that?? I know it operates unmanaged code but the performance is better ...
Is there any specialized image reading streams in .NET Framework????
Please help!
[edit]Code block added to tidy up formatting. OriginalGriff[/edit]
推荐答案
不确定是否有很大的改进,但是有 ^ ],这完全不需要您使用流.
不要从您的path属性调用Draw-而是使用Invalidate().当它不太忙于其他事情时,它将操作Paint事件.然后将e.Graphics对象传递给Paint事件中的Draw例程,这样可以节省多个图形对象的开销.
在您发布代码时,请在< pre> ...</pre>周围加上块(带有代码块"小部件)-它保留格式并使其更易于阅读.
Not sure if it is a vast improvement, but there is an Image.FromFile[^] which removes the need for you to play with the stream at all.
Don''t call Draw from your path property - use Invalidate() instead. This will action the Paint event when it''s not too busy with other things. Then hand the e.Graphics object to the Draw routine in the Paint event, which saves the overhead of multiple graphics objects.
And when you post code, surround it with <pre>...</pre> blocks (with the "code block" widget) - it preserves the formatting and makes it easier to read.
这篇关于帮我优化我的EnhancedPictureBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!