Win8Metro程式設計中,圖像相關的操作基本都是以流的形式進行的,圖像對象類型在Metro主要表現為兩種形式:BitmapImage和WriteableBitmap,圖像的顯示控件為Image。
我們可以用如下方式打開和顯示一幅圖像對象。
BitmapImage srcImage=newBitmapImage (new Uri(“UriPath”), UriKind.Relative)
其中UriPath為圖像的Uri位址,UriKind表示路徑的選擇,Urikind.Relative表示是相對路徑,也可以選擇絕對路徑Urikind.Absolute,或者Urikind.
RelativeOrAbsolute。
Image imageBox=newImage ();
imageBox.Source=srcImage;//将圖像顯示在imageBox控件中
還有一種方法則是使用WriteableBitmap對象,這也是我這裡要詳細介紹的方法。
privatestaticBitmapImage srcImage =newBitmapImage();
privatestaticWriteableBitmap
wbsrcImage;
//open image fuctiondefinition
privateasyncvoid
OpenImage()
{
FileOpenPicker imagePicker =newFileOpenPicker
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation =PickerLocationId.PicturesLibrary,
FileTypeFilter = { ".jpg",".jpeg",".png",".bmp"
}
};
Guid decoderId;
StorageFile imageFile =await
imagePicker.PickSingleFileAsync();
if (imageFile !=null)
srcImage = newBitmapImage();
using (IRandomAccessStream stream =await
imageFile.OpenAsync(FileAccessMode.Read))
{
srcImage.SetSource(stream);
switch (imageFile.FileType.ToLower())
case".jpg":
case".jpeg":
decoderId = Windows.Graphics.Imaging.BitmapDecoder.JpegDecoderId;
break;
case".bmp":
decoderId = Windows.Graphics.Imaging.BitmapDecoder.BmpDecoderId;
case".png":
decoderId = Windows.Graphics.Imaging.BitmapDecoder.PngDecoderId;
default:
return;
Windows.Graphics.Imaging.BitmapDecoder decoder =await
Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(decoderId, stream);
int width = (int)decoder.PixelWidth;
int height = (int)decoder.PixelHeight;
Windows.Graphics.Imaging.PixelDataProvider dataprovider =await
decoder.GetPixelDataAsync();
byte[] pixels =dataprovider.DetachPixelData();
wbsrcImage = newWriteableBitmap(width, height);
Stream pixelStream =wbsrcImage.PixelBuffer.AsStream();
//rgba in original
//to display ,convert tobgra
for (int i = 0; i < pixels.Length; i += 4)
byte temp = pixels[i];
pixels[i] =pixels[i + 2];
pixels[i +2] = temp;
pixelStream.Write(pixels, 0, pixels.Length);
pixelStream.Dispose();
stream.Dispose();
ImageOne.Source =wbsrcImage;
ImageOne.Width =wbsrcImage.PixelWidth;
ImageOne.Height =wbsrcImage.PixelHeight;
}
}
//save image fuction definition
SaveImage(WriteableBitmap src)
FileSavePicker save =newFileSavePicker();
save.SuggestedStartLocation =PickerLocationId.PicturesLibrary;
save.DefaultFileExtension =".jpg";
save.SuggestedFileName ="newimage";
save.FileTypeChoices.Add(".bmp",newList<string>()
{ ".bmp" });
save.FileTypeChoices.Add(".png",newList<string>()
{ ".png" });
save.FileTypeChoices.Add(".jpg",newList<string>()
{ ".jpg",".jpeg" });
StorageFile savedItem =await
save.PickSaveFileAsync();
try
Guid encoderId;
switch (savedItem.FileType.ToLower())
case".jpg":
encoderId =BitmapEncoder.JpegEncoderId;
break;
case".bmp":
encoderId =BitmapEncoder.BmpEncoderId;
case".png":
default:
encoderId =BitmapEncoder.PngEncoderId;
IRandomAccessStream fileStream =await savedItem.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
BitmapEncoder encoder =awaitBitmapEncoder.CreateAsync(encoderId,
fileStream);
Stream pixelStream =src.PixelBuffer.AsStream();
byte[] pixels =newbyte[pixelStream.Length];
pixelStream.Read(pixels, 0, pixels.Length);
//pixal format shouldconvert to rgba8
byte temp = pixels[i];
pixels[i] =pixels[i + 2];
pixels[i + 2] =temp;
encoder.SetPixelData(
BitmapPixelFormat.Rgba8,
BitmapAlphaMode.Straight,
(uint)src.PixelWidth,
(uint)src.PixelHeight,
96, // Horizontal DPI
96, // Vertical DPI
pixels
);
await encoder.FlushAsync();
}
catch (Exception
e)
throw e;
最後,分享一個專業的圖像處理網站(微像素),裡面有很多源代碼下載下傳:
<a href="http://www.zealpixel.com/portal.php">http://www.zealpixel.com/portal.php</a>