Conversion of image formats in windowsphone7

Source: Internet
Author: User
Tags bmp image

The bitmapimage and image interfaces in windowsphone7 and siverlight are quite different. In general, siverlight is more encapsulated.

When compressing bitmap, siverlight provides functions for setting and accessing image DPI (dots per inch) and pixel, but WP7 can only access and operate pixel. In this case, for compressing an image in WP7, you can easily increase or decrease pixels, but it is difficult to access its DPI, it is difficult to customize the actual width and height of the image.

The following is a conversion class for image and file stream formats. For details, refer:

Public class imageutil {// <summary> // convert the image to a binary stream // </Summary> /// <Param name = "sourceimage"> </Param >/// <returns> </returns> Public static byte [] imagetobytearray (image sourceimage) {return imagesourcetobytearray (sourceimage. source );} /// <summary> /// convert the imagesource to a binary stream // </Summary> /// <Param name = "sourceimage"> </param> /// <returns> </returns> Public static byte [] imagesourcetobyte Array (imagesource source) {bitmapsource BS = (bitmapsource) source; using (memorystream MS = new memorystream () {writeablebitmap = new writeablebitmap (BS); extensions. savejpeg (writeablebitmap, MS, BS. pixelwidth, BS. pixelheight, 0,100); Return Ms. getbuffer ();}} /// <summary> /// convert binary data to image format. // </Summary> /// <Param name = "bitdata"> </param> // /<returns> </returns> Public s Tatic image bytearraytoimage (byte [] bitdata) {image = new image (); image. source = bytearraytobitmapimage (bitdata); Return image ;} /// <summary> /// convert binary data to an image in bitmapimage format. /// </Summary> /// <Param name = "bitdata"> </param> // /<returns> </returns> Public static bitmapimage bytearraytobitmapimage (byte [] bitdata) {bitmapimage bitmap = new bitmapimage (); Using (memorystream MS = new memorystre Am (bitdata) {bitmap. createoptions = bitmapcreateoptions. delaycreation; bitmap. setsource (MS); Return bitmap ;}} /// <summary> /// write stream data to a jpeg file /// </Summary> /// <Param name = "stream"> </param> /// <Param name = "filedirectory"> </param> /// <Param name = "FILENAME"> </param> /// <Param name = "targetwidth"> </ param> // <Param name = "targetheight"> </param> Public static void writestreamobjecttojpeg (Stream Stream, string filedirectory, string filename, int targetwidth, int targetheight) {using (isolatedstoragefile store = isolatedstoragefile. getuserstoreforapplication () {If (! Store. directoryexists (filedirectory) {store. createdirectory (filedirectory);} writeablebitmap BMP image = new writeablebitmap (targetwidth, targetheight); BMP image. setsource (Stream); Using (isolatedstoragefilestream isostream = store. openfile (filedirectory + '/' + filename, filemode. openorcreate) {system. windows. media. imaging. extensions. savejpeg (BMP image, isostream, targetwidth, targetheight, 0,100 );}}} /// <summary> /// write the JPEG file to the data stream // </Summary> /// <Param name = "filepath"> </param> // /<returns> </returns> Public static bitmapimage readbitmapimagefromjpeg (string filepath) {bitmapimage image = new bitmapimage (); stream source = readstreamfromjpeg (filepath); image. setsource (source); Return image;} public static stream readstreamfromjpeg (string filepath) {using (isolatedstoragefile store = isolatedstoragefile. getuserstoreforapplication () {using (isolatedstoragefilestream isostream = store. openfile (filepath, filemode. open) {stream filestream = new memorystream (); isostream. copyto (filestream); isostream. close (); isostream. dispose (); store. dispose (); Return filestream ;}}}}

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: info-contact@alibabacloud.com 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.