473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

PictureBox control memory and speed BIG problem

I’m writing a Windows application.
In the form I have a Panel and inside the panel I have a PictureBox control.
I’m loading the PictureBox control with BMP image that has the following
criteria:
14174 x 7874 Pixels (111.61MPixels), 1 BitsPerPixel, 3200x3200 DPI, No
Compression.
The memory consumption for loading this image should be 13.31 MB (13,952,776
Bytes) (this is what IrfanView takes, http://www.irfanview.com/).

I’m loading the BMP file like that:
pictureBox1.Image = Image.FromFile("MyFileName.bmp", true);
// - or -
//pictureBox1.Image = Image.FromFile("MyFileName.bmp");
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Width = pictureBox1.Image.Width;
pictureBox1.Height = pictureBox1.Image.Height;

And this way, I get image scrolling for free (see
http://www.codeproject.com/cs/miscctrl/PictureBox.asp).

After I load the image, the memory usage of my application is about 50 MB
according to the Windows ‘Task Manager’.

The trouble is that every time I try to move any scroll bar, or showing the
image after hiding it (or part of it) with another window, it redraws itself
very very slowly and the memory usage jump up to 430 MB for a few seconds.

I’m running this application on a Pentium 4 processor 2.4GHz and 1.5 GB RAM.
Can anybody tell me why this is happening and how can I make it run much
much faster with no memory eating?
--------
Thanks
Sharon
Nov 17 '05 #1
5 5732
Hi Sharon,

When you load the image into memory, GDI+ is allocating the memory it needs
to store the image. Then, when you start to draw to the image, it creates
a new image buffer of the same size (hence the doubling of the memory).

There isn't much you could do here, unless you can break the image down
into smaller images and process those.

You can also try to post in microsoft.public.win32.programmer.gdi newsgroup.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #2
Hi Kevin,

I can leave with twice the memory for every redraw, but it’s eating 10 times
the amount of memory for every redraw (from less then 50 MB to 430 MB).

I suspect that for every redraw it convert the image from 1 bit per pixel to
1 byte per pixel, therefore multiplying the memory consumption at least by 8.
It this is what happening, it’s very bad. It means that the PicturePox
control can work only with very small images.

But what about larger images? Isn’t there a .NET control that I can use for
them?
--------
Thanks
Sharon
Nov 17 '05 #3

"Sharon" <Sh*****@newsgroups.nospam> wrote in message
news:85**********************************@microsof t.com...
Hi Kevin,

I can leave with twice the memory for every redraw, but it’s eating 10
times
the amount of memory for every redraw (from less then 50 MB to 430 MB).

I suspect that for every redraw it convert the image from 1 bit per pixel
to
1 byte per pixel, therefore multiplying the memory consumption at least by
8.
It this is what happening, it’s very bad. It means that the PicturePox
control can work only with very small images.

But what about larger images? Isn’t there a .NET control that I can use
for
them?
--------
Thanks
Sharon

A redraw of a loaded image does not change the memory consumption.
As I told you before, when you call Image.FromFile(), GDI+ loads the
Filedata into memory and builds an internal Image object from it (done by
GDI+ function - GdipImageForceValidation()), that means that you need aprox.
twice the memory (depending the pix. format and type of the file).

If you don't want this to happen, you can load the image from a stream like
this:

fs = new FileStream(value, FileMode.Open, FileAccess.ReadWrite);
Image = Image.FromStream(fs, false, false); // set third arg. to false to
prevent GdipImageForceValidation to be called.

This has the advantage that the internal buffer is not created and the
memory taken depends on the size of the portion actually drawn, but the
disadvantage is that it's terrible slow when redrawing (scrolling etc...),
because the drawing engine has to read the pixeldata from the underlying
stream and convert it to a bitmap each time the "view" changes.

Anyway, it looks like you are trying to use the PictureBox control for other
purposes than it was designed for, that is being a static container for
simple reasonable sized images (smaller than the form or containing
control). What you are trying is to do is use this control to display much
larger images and hope that you can scroll real fast when the control area
is smaller than the image size.
I don't think you will find a .NET control that fits this purpose, I doubt
that such control can even be built using GDI+.

Willy.


Nov 17 '05 #4
Hello again Willy,

Thanks for your help.

I changed my code to load the image from stream as you posted and indeed it
is working smoothly with image I mentioned.
But now, it takes much more then the twice the memory, it consumes 437 MB
(20 MB before loading the image, and 457 MB after loading the image).
As I mentioned before, the image is BMP 14174 x 7874 pixels (111.61
MPixels), 1 bits per pixel, 3200x3200 DPI, No Compression and the memory
consumption for loading this image should be 13.31 MB.

So I tested to se its limit I tried to load the same image but sized 15748 x
28348 pixels (should take 53.23 MB (55,810,960 Bytes)), but I get
successfully exception (strange one): System.ComponentModel.Win32Exception
{The operation completed successfully}
Thrown by the
System.Windows.Forms.DibGraphicsBufferManager.Crea teCompatibleDIB(IntPtr hdc,
IntPtr hpal, Int32 ulWidth, Int32 ulHeight, IntPtr& ppvBits)

Do you know that one?

I guess that the control convert the image from 1 bit per pixel to 1 byte
per pixel, it’s the only explanation I can imagine for that amount of memory
consumption. Still it doesn’t explain the so large memory it takes.

Can you elaborate on that?

If I can not use the PictureBox control for this kind of images, how can I
do that in any other way?
---------
Thanks
Sharon
Nov 17 '05 #5
Hi Sharon,

In this case, you might need some third party components which have better
performance on large pictures. Or you have to write your own control to
display and scroll.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CroDude | last post by:
Hi all! I've made a little app that creates thumbnails and if user clicks on it, it displays the original picture in the picturebox on a panel control. In a thumbnail mouse down event I'm using...
7
by: kebalex | last post by:
Hi, I have an app (written in .NET 2.0) which updates a picturebox according to some user input (a slider control). when the user makes a change i loop through all of the pixels, do a...
4
by: kimiraikkonen | last post by:
Hi, On my system which is 2.4GHZ P4 CPU, 1GB memory + 64MB DDR graphic card, if i create a simple picturebox docked on a form sized about 500x350 or less or more, doesn't matter, and also if i...
3
by: kirk | last post by:
I have a form with a PictureBox control on it. The .Image property is set to a PNG file(which shows the picture of the US map) with some transparency in it. The .BackColor property is set to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.