Silverlight converts a picture to a byte

Source: Internet
Author: User
Tags silverlight


It's been a long time no blog has been written ~ ~ ~, more and more lazy, more and more no target!


Recently people have been asked how to upload pictures through Silverlight and save the background server? As we all know, Silverlight is a client program, not very good "communication" with the server, the way to upload pictures is generally through the following process:

1, the client obtains the picture-->2, the conversion can transmit the data-->3, transmits the data to the server side-->4, the service side restores the data as the picture

The first and fourth steps are no problem, the problem is mostly in the second and third steps, how to get the client and the server can be recognized and easily transmitted data?

If you are using both the client and the server. NET scheme, which is very good to do, can be done by the following methods:

1, the client has been selected by the customer FileInfo, and then through the WebClient method post to the server, the server and then restore into pictures, detailed article /02/silerlight-up-down.html

2. The client converts the image file selected by the customer into a FileStream package in a class passed through WCF to the service side, and the server is restored, detailed article HTTP://WWW.CNBLOGS.COM/GAOWEIPENG/ARCHIVE/2009/09/13 /1565409.html

Some students of the reunion scratching asked, there is no one to use Silverlight to pass to similar java,php situation? Of course, and I recently encountered is this similar situation, in advance to declare that I do not really understand Java and PHP, I can only convert the picture into a byte to you, as to how you restore, I can only say: "Classmate, any heavy and way far!" "No nonsense, see the analysis below:

Before walking a lot of detours, always thinking of converting writeablebitmap into byte (also do not know who is going to let WriteableBitmap turn, t_t), and then I found that there is no need so troublesome, directly on the code:

byte[] staticbyte;//global byte, to convert byte to image        Private voidOpenimage () {OpenFileDialog ImageFile=NewOpenFileDialog () {Filter="Jpeg Files (*.jpg) |*.jpg| All Files (*. *) |*.*"            }; if(Imagefile.showdialog () = =true) {System.IO.Stream FileStream=ImageFile.File.OpenRead (); byte[] imagebyte=New byte[Filestream.length];//sets the length of a byte to the length of the file streamFileStream.Read (Imagebyte,0, imagebyte.length);//reads the file stream information into a byte arrayStaticbyte = Imagebyte;//saves a well-read byte array to the globalBytetextbox.text=utf8encoding.utf8.getstring (Imagebyte,0, imagebyte.length);//displays a byte array to the foreground            }        }

Directly through the FileStream.Read method to convert the file into Byte,ok completion!

I added a bit of code later to convert byte into a picture, the code is as follows:

        privatevoid button1_click (object  sender, RoutedEventArgs e)        {            Openimage ();             New MemoryStream (Staticbyte,0, staticbyte.length); // convert byte to memory stream            New BitmapImage ();            Img. SetSource (MemoryStream); // set the source for BitmapImage to memory stream            this. Image1. Source = img; // Show Picture        }
Program picture

Select the Image interface

Effect interface

Program source Code

Written using vs2010 sp1+silverlight5, Silverlightimagetobyte

Like please recommend, reprint please indicate the source

Silverlight converts a picture to a byte

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.