can you help me

Dec 26, 2010 at 5:00 PM
Edited Dec 26, 2010 at 5:08 PM

hello  thanks for your concern.

I'm a freshmen  in SilverLight and C#, i don't know Where i'm wrong . I am look to you for help.

i am trying to build a Windows phone 7 Solution,  I wanna Save a WritableBitmap to a PNG file ( in isolated storage ), and use the Image Control to show the png file.

first, I  add the reference:

ImageTools.Utils  ImageTools.IO.Png  ImageTools

second ,I add the namespace:

using ImageTools;
using ImageTools.IO;
using ImageTools.IO.Png;

Third, I add the code :

I have got the WritableBitmap (myBitmap).

Decoders.AddDecoder<PngDecoder>();
Encoders.AddEncoder<PngEncoder>();
PngEncoder pngCoder = new PngEncoder();

MemoryStream pngStream = new MemoryStream();
ExtendedImage myImage;
myImage = myBitmap.ToImage();

pngCoder.Encode(myImage, pngStream);

//  save to png file in Isolated storage file

var myStore = IsolatedStorageFile.GetUserStoreForApplication();
myStore.CreateDirectory("mystorage");

 IsolatedStorageFileStream myFileStream = myStore.CreateFile("mystorage\test.PNG");
StreamWriter filewriter = new StreamWriter(myFileStream);
filewriter.WriteLine(myFileStream);
myFileStream.Close();

// Then read the Png file and show

IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamReader fileReader = null;

fileReader = new StreamReader(new IsolatedStorageFileStream("mystorage\\test.PNG", FileMode.Open, fileStorage));
Stream pngSaveStream = fileReader.BaseStream;
PngDecoder pngDecoder = new PngDecoder();

ExtendedImage myImage = new ExtendedImage();

pngDecoder.Decode(myImage, pngStream); // there was always a Exception

WritableBitmap mybitmap2=myImage.ToBitmap(); //there was always a Exception

image1.Source = mybitmap2;

/*************

Can I Use it like this?  I am so  sorry, may be my question is too easy, I am studying to use SilverLight and C#.

and I don't know too much about XAML, I Used to program with C and C++.

Can you show me how should i use Imagetools.  and I hope you can give me a simple example to use ImaeTools in windows Phone 7 (my Email: wfwang1985@sina.com)

I have seen  the examples in  Source Code Column , but I can't understand it , When I read through the Code , I still don't know  How I

Can use ImageTools in my Solution.

Thank you for your attention. I am looking for your reply. 

***************************/


 

Dec 27, 2010 at 3:46 PM

Anybody can help me ?

Coordinator
Dec 28, 2010 at 10:37 AM

Hi,

my todo list is so long that I cannot see the end. Therefore I cannot help you with a full example. Please have a look to the demo application. Download the source code to have a look.

I assume that saving is working.

 

To load a image you dont need a streamreader, just pass the stream to the decoder.

Or better add the png decoder (http://imagetools.codeplex.com/wikipage?title=Working%20with%20different%20image%20formats&referringTitle=Home) and use:

 

ExtendedImage myImage = new ExtendedImage();
myImage.SetSource(myStream);

Please tell me what exception do you get.

Dec 28, 2010 at 6:28 PM
Edited Dec 28, 2010 at 7:32 PM

Thanks for your reply. I Wanna Save the WritableBitmap to IsolatedStorage in PNG format, and load the png image in the isolatedstorage. I have read the demo application ,but the application is based on SL4,not on phone , the demo on phone is too simple , there is no code in the MainPage.xaml.cs .
//first I encode the Writablebitmap to PNGStream like this: i add the decoder first
                                                                                                      
PngEncoder pngCoder = new PngEncoder();
MemoryStream pngStream = new MemoryStream();
ExtendedImage myImage;
myImage = myBitmap.ToImage();
pngCoder.Encode(myImage, pngStream);// can i convert the Writablebitmap to png Stream like this??

//can I save a Writablebimap to IsolatedStorage in png format like this, the method in the first post, is not working
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
myStore.CreateDirectory("test");
IsolatedStorageFileStream myFileStream = myStore.CreateFile("test\\test.PNG");
myImage.WriteToStream(myFileStream, "test\\test.PNG");

//When i get the png stream ,I try this:
ExtendedImage myImage = new ExtendedImage();
myImage.SetSource(pngStream);  // the myImage vale likes this :Image: 0x00000000x0x00000000 _uriSource null System.Uri DensityX 75.0 double,the pngStream is not null.
WriteableBitmap mybitmap2 = myImage2.ToBitmap(); //there is a exception  "ArgumentException was unhandled, Image has not bee loaded."

//then I try this : there was a png file in my Isolated storage  "test\\test.PNG"              
ExtendedImage myImage = new ExtendedImage();
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplicatio();
myImage.SetSource(fileStorage.OpenFile("test\\test.jpg", FileMode.Open,FileAccess.ReadWrite));//there is also a Exception "ArgumentException was  unhandled , Image has not bee loaded."

 thank you very much! I am dying to get the help from you , 

 

Coordinator
Dec 28, 2010 at 7:22 PM
Both methods to write a stream should work.

Loading the image is working asynchronously. subscribe to the
LoadingCompleted and make any further operations after this event has
raised.

The API for Silverlight und Windows Phone is the same, so you can have a
look the the SL Test App too.
Dec 29, 2010 at 4:52 PM

Thank you so much , it's working ,but there is another problem.

 when the program run in  Image_LoadingCompleted function, It will jump to Image_LoadingFailed fuction , the Exception occurs.( Invalid cross-thread access). it seems to be I can not use the ToBitmap() method.

        private void Image_LoadingFailed(object sender, EventArgs e)
        {
            MessageBox.Show("Load Image Failed"); //there is an UnauthorizedAccessException, Invalid cross-thread access.
        }
        private void Image_LoadingCompleted(object sender, EventArgs e)
        {
            ExtendedImage EImage = sender as ExtendedImage;
            WriteableBitmap mybitmap=EImage.ToBitmap(); // it will  jump to the  Image_LoadingFailed fuciton
            image2.Source = mybitmap2;  // can not run to here
        }

Coordinator
Dec 29, 2010 at 5:00 PM
The code in the Image_LoadingCompleted Method is still running in the seperate thread.

Therefore you cannot access or create any gui elements in this thread (e.g. WriteableBitmap). But you can use the dispatcher:

private void Image_LoadingCompleted(object sender, EventArgs e)
{
ExtendedImage EImage = sender as ExtendedImage;

Dispatcher.BeginInvoke(() =>
{
// Running in GUI Thread
WriteableBitmap mybitmap=EImage.ToBitmap();

image2.Source = mybitmap2; // can not run to here

}
}

But there is also a control called AnimatedImage (which can also handle non animated images), which will handle this for you.

Just create a new image, load it and pass it to this control:

ExtendedImage myImage = sender as ExtendedImage;
myImage.SetSource(stream);

AnimatedImage.Source = myImage;

This is much simpler isnt it? The extended image is designed not to have any dependencies to controls, therefore you have to do everything by yourself.

Dec 29, 2010 at 5:16 PM

thanks for your help!!

I have hadled this Problem, I should use the ToBitmap method in MainUI thread.

thanks for your greate patience, thanks for your great work!

Express my appreciation once again!