Win8 Metro(C#) 数字图像处理--1 图像打开,保存

时间:2021-10-20 19:36:57

作为本专栏的第一篇,必不可少的需要介绍一下图像的打开与保存,一便大家后面DEMO的制作。

  Win8Metro编程中,图像相关的操作基本都是以流的形式进行的,图像对象类型在Metro主要表现为两种形式:BitmapImageWriteableBitmap,图像的显示控件为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对象,这也是我这里要详细介绍的方法。

1.图像打开

        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;

                           break;

                       case".png":

                           decoderId = Windows.Graphics.Imaging.BitmapDecoder.PngDecoderId;

                           break;

                       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;

            }

        }

2.图像保存

//save image fuction definition

        privateasyncvoid 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;

                       break;

                    case".png":

                    default:

                       encoderId =BitmapEncoder.PngEncoderId;

                       break;

                }

                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

                for (int i = 0; i < pixels.Length; i += 4)

                {

                    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;

            }

        }

最后,分享一个专业的图像处理网站(微像素),里面有很多源代码下载: http://www.zealpixel.com/portal.php