Path to Windows Phone development (14) Loading bitmap

Source: Internet
Author: User

In addition to text, bitmap is one of the most common objects in Silverlight. We usually define it as a two-dimensional Bit Array corresponding to the pixels of the graphic display device.
The extension of Windows native bitmap files is BMP, but it has not dominated in recent years, and the compression format has become popular. Currently, the three most popular bitmap formats are:

  • JPEG (Joint photography Experts Group, joint image Expert Group)
  • PNG (Portable Network Graphics, portable network image)
  • GIF (Graphics Interchange File)

Silverlight only supports JPEG and PNG formats.

Load local bitmap

  1. Use the Image Element in Silverlight to load the local bitmap

XAML code:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image Source="images/photo.jpg"
Stretch="None"/>
</Grid>

Note: Stretch = "NONE" indicates that the bitmap is displayed in the original size.
Effect

  2. Use the C # code to load the local bitmap

XAML code:

<Grid X: Name = "contentpanel" grid. Row = "1" margin = "12,0, 12,0">
<Button name = "btnload"
Content = "using C # code to load local bitmap"
Horizontalalignment = "center"
Verticalalignment = "TOP"
Click = "btnload_click"/>
<Image name = "IMG"
Stretch = "NONE"/>
</GRID>

C # code:

Public partial class mainpage: phoneapplicationpage
{
// Constructor
Public mainpage ()
{
Initializecomponent ();
}

Private void btnload_click (Object sender, routedeventargs e) // click the event button
{
// URI uri = new uri ("/silverlighttaptoload; component/images/photo.jpg", urikind. Relative); // create a URI object. At this time, the build action of the bitmap is resource.
Uri uri = new uri ("images/photo.jpg", urikind. Relative); // The build action of the bitmap is content

Streamresourceinfo resourceinfo = application. getresourcestream (URI); // call the getresourcestream method of the application class to access the resource file and return streamresourceinfo
Bitmapimage BMP = new bitmapimage (); // create a bitmapimage object as the data source
BMP. setsource (resourceinfo. Stream); // call the setsource method to obtain the stream in the Resource
IMG. Source = BMP; // sets the bitmap data source.
}
}

Effect

Operation mobile phone Image Library

The following example uses the photochoosertask selector to view images in the mobile phone Image Library.

XAML code:

<Grid X: Name = "layoutroot" background = "Transparent">
<Button name = "btnphotochooser"
Content = "image selector"
Horizontalalignment = "center"
Verticalalignment = "TOP"
Padding = "0, 34"
Click = "btnphotochooser_click"/>
<Image name = "imgchooser"/>
</GRID>

C # code:

Public partial class mainpage: phoneapplicationpage
{
Photochoosertask choosephoto = new photochoosertask (); // create a photochoosertask object

// Constructor
Public mainpage ()
{
Initializecomponent ();

This. choosephoto. Completed + = choosephoto_completed; // registers the event handler to obtain the result of the selector operation.
}

Private void btnphotochooser_click (Object sender, routedeventargs e) // click the event button
{
Choosephoto. Show (); // start the image selector.
}

Void choosephoto_completed (Object sender, photoresult e) // The event handler. The photoresult class indicates the image returned by calling photochoosertask.
{
If (E. taskresult = taskresult. OK) // The task is successfully completed. taskresult indicates whether the task is completed.
{
Completechoosertask (E); // call the method to display the image
}
}

Protected void completechoosertask (photoresult E)
{
VaR BMP = new bitmapimage (); // create a bitmapimage object as the data source of the image element. It is equivalent to bitmapimage BMP = new bitmapimage ()
BMP. setsource (E. chosenphoto); // call the setsource method to obtain the data stream of the photo.
This. imgchooser. Source = BMP; // sets the data source display image of the image element.
}
}

Effect

Related Article

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.