// Instantiate the selector photochoosertask = new photochoosertask (); bitmapimage bimg; int newpixelwidth; int newpixelheight; // read the input value from the page in the constructor as the target resolution newpixelheight = int. parse (textbox2.text. tostring (); // read the input value from the page as the target resolution newpixelheight = int. parse (textbox2.text. tostring (); newpixelwidth = int. parse (textbox1.text. tostring (); textbox1.textchanged + = new textchangedeventhandler (textbox1_textchang Ed); textbox2.textchanged + = new textchangedeventhandler (textbox2_textchanged); void textbox2_textchanged (Object sender, textchangedeventargs e) {If (textbox2.text! = "" & Int. parse (textbox2.text. tostring ())! = 0) {newpixelheight = int. parse (textbox2.text. tostring () ;}} void textbox=textchanged (Object sender, textchangedeventargs e) {If (textbox1.text! = "" & Int. parse (textbox1.text. tostring ())! = 0) {newpixelwidth = int. parse (textbox1.text. tostring () ;}} private void button#click (Object sender, routedeventargs e) {// create a stream streamresourceinfo Sri = NULL for the JPEG file; // set the build action of tulips.jpg to content URI uri = new uri ("images/tulips.jpg", urikind. relative); Sri = application. getresourcestream (Response URI); // decodes the stream writeablebitmap bitmap = picturedecoder. decodejpeg (SRI. stream); IMG. source = bitmap;} private void button2_click (Object sender, routedeventargs e) {string tempjpg = "tempimg"; // create virtual storage var mystore = isolatedstoragefile. getuserstoreforapplication (); If (mystore. fileexists (tempjpg) {mystore. deletefile (tempjpg);} isolatedstoragefilestream myfilestream = mystore. createfile (tempjpg); streamresourceinfo Sri = NULL; SRI = application. getresourcestream (New uri ("images/tulips.jpg", urikind. relative); // create a writeablebitmap object, bitmapimage bitmap = new bitmapimage (); bitmap. createoptions = bitmapcreateoptions. none; bitmap. setsource (SRI. stream); writeablebitmap WB = new writeablebitmap (Bitmap); // writeablebitmap object encoding, set the target resolution WB. savejpeg (myfilestream, newpixelwidth, newpixelheight, 0, 85); myfilestream. close (); myfilestream = mystore. openfile (tempjpg, filemode. open, fileaccess. read); // Save the image to the image library medialibrary library = new medialibrary (); picture PIC = library. savepicture ("savedpicture.jpg", myfilestream); myfilestream. close ();} private void button3_click (Object sender, routedeventargs e) {// open the image library photochoosertask. show (); photochoosertask. completed + = new eventhandler <photoresult> (photochoosertask_completed);} void photochoosertask_completed (Object sender, photoresult e) {If (E. taskresult = taskresult. OK) {bimg = new bitmapimage (); bimg. setsource (E. chosenphoto); // sets the IMG data source of the image control. source = bimg ;}}
UI
<Grid X: Name = "layoutroot" background = "Transparent"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition Height = "*"/> </grid. rowdefinitions> <! -- Titlepanel contains the name of the application and page title --> <stackpanel X: Name = "titlepanel" grid. row = "0" margin = ","> <textblock X: name = "applicationtitle" text = "my application" style = "{staticresource phonetextnormalstyle}"/> <textblock X: name = "pagetitle" text = "Page name" margin = "9,-7,0, 0" style = "{staticresource phonetexttitle1style}"/> </stackpanel> <! -- Contentpanel-place additional content here --> <grid X: Name = "contentpanel" grid. row = "1" margin = "12, 0, 12, 0"> <image X: name = "IMG" Height = "300" verticalalignment = "TOP"/> <button content = "load image" Height = "82" horizontalalignment = "Left" margin = ", 0, 0,219 "name =" button1 "verticalignment =" bottom "width =" 160 "Click =" button#click "/> <button content =" Save image "Height =" 72 "horizontalalignment =" left "margin =" 150,376, 160 "name =" button2 "verticalignment =" TOP "width =" "Click =" button2_click "/> <button content =" view image "Height =" 72 "horizontalalignment =" left "margin =" 150,438, 160 "name =" button3 "verticalignment =" TOP "width =" 150,535 "Click =" button3_click "/> <textbox Height =" 72 "horizontalalignment =" Left "margin = ", 114 "name =" textbox1 "text =" 20 "verticalalignment =" TOP "width =" 251,535 "/> <textbox Height =" 72 "horizontalalignment =" Left "margin =", 116 "name =" textbox2 "text =" 20 "verticalalignment =" TOP "width =" "/> <textblock Height =" 46 "horizontalalignment =" Left "margin = "- 12,555, 156 "name =" textblock1 "text =" set target resolution "verticalignment =" TOP "width =" "/> </GRID>
Getting started with WP8 to mastering imagecompress Image Compression