views:

31

answers:

3

The topic says it all.

Using Compact Framework C#

I'm tiling (order/sequence is important) some images that i download from an url, into a Panel(each image is a PictureBox). This can be a huge process, and may take some time.

Therefor i only want the user to download the images and tile them once. So the next time the user uses the Tile Application, the Panel that was created the first time is already stored in a file and is loaded from that file.

So what i want is a method to store a Panel as a file.

Is this possible, or do you think i should do it another way?

I've tried something like this:

            BinaryWriter panelStorage = new BinaryWriter(new FileStream("imagePanel.panel", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
            Byte[] bImageObject = new Byte[20000];
            bImageObject = (byte[])(object)this.imagePanel;
            panelStorage .Write(bMapObject);
            panelStorage .Close();

But the casting was not very legal :P

"InvalidCastException"

Can anyone help me with this problem?

Thank you in advance!

+1  A: 

Yes, your code will throw, you cannot cast a Control object to byte[]. That kind of conversion requires serialization, the kind performed by BinaryFormatter. Trouble is: the Control class and its decedents are not serializable, they don't have the [Serializable] attribute.

The Control class is a complicated mother, it has hundreds of private fields. A lot of those fields have values that are generated at runtime, often by interoperating with the native Windows window. While serializing them is somewhat possible, deserializing is not. Reconstructing the Control object requires the native window to be reconstructed as well. And it has a lot of state itself, the kind that's set by sending it a bunch of message. Regenerating those messages isn't practical.

This doesn't leave you with a lot of attractive options. Serializing a UI is possible, the Windows Forms designer and WPF XAML does it. But these are very large chunks of code, not easily embedded in your program.

Aim lower, don't try to serialize the entire control. Just some of its properties, like Size and Location, and you'll have a shot at making this work.

Hans Passant
Thank you for describing and insightful information!I'll try to serialize the class with properties and images.Thank you for the answer :)
A: 

ResX files serialize several properties of the control. Perhaps you can use those?

psychotik
I'll look into that, but i doubt that it is more helpful than just making a class and serialize the class.
+1  A: 

I wouldn't serialize the control if I were you. I'd instead serialise the data that goes into the control. There are a couple of reasons.

Firstly, it's easier. You can take whatever data you are putting in (images) and put them into a custom MyPanel class or whatever. Store information about how they are manipulated in there too, and then you can serialise that class, and it's done.

The second reason is that if you want to change how the data is displayed or used at a later date, you have a lot more freedom. You can use it in other ways, or with other applications and controls without any real effort on your part.

SLC
I have thought about doing that, but it seems like it is the insertion of pictureboxes in the panel that require the power. I've had only pictureboxes, without images, and it take some time to load them into the panel.Could it be becase the panel can get over 9000px in width and height, because of the pictureboxes i insert?I will anyway try your method.Thanks!
It could be, if you explain in a little more detail what you're trying to do, we might be able to offer better suggestions. If you have multiple picturebox controls, that is where the problem will be though. Doing lots of graphics manipulation, you'd be better off using GDI - that is, you have a single picturebox (or you may be able to use the panel) as a drawing surface, and you draw directly to it using the OnPaint method of your form. It is considerably faster.
SLC
I have a LARGE image, which i have tiled up to many smaller images. (This is done serverside).I want to show the whole image on the windows mobile, and have thought something like this:If the image is 4x4 tiles:- I need fake tiles around it(i call them wall tiles, since they surround the whole image) so that the user will be able to move around on the picture(The movement is done by setting scrollbar position, when the user touches and drags on the screen).
- I need the panel to be a bit outside the screen, so that the scrollbars is not visible.- The panel won't resize it self if i not put any elements in there, that is why i have left out drawing on the panel.
Here is an example on how my tiling is:http://img638.imageshack.us/img638/2076/examplec.pngEach picturebox is 256x256px, so therefor the screen will only show maybe 2 or 3 tiles at a time. (The plan is to load the images when the user moves along the panel).But the panel NEEDS to be resized so that the scrollbars will "appear", and that the wall tiles is created and "touchable"
Maybe i can just add one single picturebox that has the size of all the tiles that im going to load, add that picturebox to the panel, and voila the panel is that big. I think you gave me something here SLC :). By doing this i can use GDI and draw on the one picturebox.
Now i just have to learn how to use GDI and OnPaint :)I have tried that once. One more question then:How would you store the images that i download from the urls and the pixels they should be painted on, so that the OnPaint method would work desirable?
Answer to myselft: ArrayList or HashTable. Correct me if i'm wrong
If you want to learn GDI on mobile devices, I recommend a superb article written by one of my lecturers at University, now available on MSDN here - "Games Programming With Cheese" - http://msdn.microsoft.com/en-us/library/aa446511.aspx - it covers all the basics you need and it's great fun too. Once you've finished this you should have no trouble at all :)
SLC
Thanks a lot!Tested by storing the bitmaps in hashtable with coordinates, and it works like a dream... so now i only need to to the GDI draw :)
Got another problem regarding the GDI and OnPaint:http://stackoverflow.com/questions/2566705/how-to-get-the-drawn-image-away-from-the-screen-when-i-have-scrolled-down-the-pa