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

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

  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对象,这也是我这里要详细介绍的方法。

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;

            }

        }

最后,分享一个专业的图像处理网站(微像素),里面有很多源代码下载:

Win8 Metro(C#) 数字图像处理--1 图像打开,保存的更多相关文章

  1. Win8 Metro(C#)数字图像处理--4图像颜色空间描述

    原文:Win8 Metro(C#)数字图像处理--4图像颜色空间描述  图像颜色空间是图像颜色集合的数学表示,本小节将针对几种常见颜色空间做个简单介绍. /// <summary> / ...

  2. Win8 Metro(C#)数字图像处理--3.2图像方差计算

    原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...

  3. Win8 Metro(C#)数字图像处理--3.3图像直方图计算

    原文:Win8 Metro(C#)数字图像处理--3.3图像直方图计算 /// <summary> /// Get the array of histrgram. /// </sum ...

  4. Win8 Metro(C#)数字图像处理--3.4图像信息熵计算

    原文:Win8 Metro(C#)数字图像处理--3.4图像信息熵计算 [函数代码] /// <summary> /// Entropy of one image. /// </su ...

  5. Win8 Metro(C#)数字图像处理--3.5图像形心计算

    原文:Win8 Metro(C#)数字图像处理--3.5图像形心计算 /// <summary> /// Get the center of the object in an image. ...

  6. Win8 Metro(C#)数字图像处理--2.73一种背景图像融合特效

    原文:Win8 Metro(C#)数字图像处理--2.73一种背景图像融合特效 /// <summary> /// Image merge process. /// </summar ...

  7. Win8 Metro(C#)数字图像处理--3.1图像均值计算

    原文:Win8 Metro(C#)数字图像处理--3.1图像均值计算 /// <summary> /// Mean value computing. /// </summary> ...

  8. Win8 Metro(C#)数字图像处理--2.74图像凸包计算

    原文:Win8 Metro(C#)数字图像处理--2.74图像凸包计算 /// <summary> /// Convex Hull compute. /// </summary> ...

  9. Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器

    原文:Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器 /// <summary> /// Min value filter. /// </summary> ...

随机推荐

  1. MVC4中AJAX Html页面打开调用后台方法实现动态载入数据库中的数据

    之前一直用window.onload方法来调用js方法来实现,今天纠结能不能换个方法实现. 非常明显是能够的. 在html前台页面引用js代码例如以下 @Scripts.Render("~/ ...

  2. Android使用READ_CONTACTS读取手机联系人

    实例代码: package com.example.readcontacts; import java.io.InputStream; import java.util.ArrayList; impo ...

  3. C++ 快速入门笔记:面向对象编程

    类 & 对象 类定义 class Box { public: double length; // Length of a box double breadth; // Breadth of a ...

  4. 【codeforces 765B】Code obfuscation

    [题目链接]:http://codeforces.com/contest/765/problem/B [题意] 让你把每个变量都依次替换成a,b,c,-.d这些字母; 且要按顺序先用a再用b-.c.d ...

  5. KVO的使用(1)

    1.在某个类中添加下面方法: -(void)viewWillAppear:(BOOL)animated{ [[NSNotificationCenter defaultCenter] addObserv ...

  6. UWP 和 WPF 对比

    原文:UWP 和 WPF 对比 本文告诉大家 UWP 和 WPF 的不同. 如果在遇到技术选择或者想和小伙伴吹的时候可以让他以为自己很厉害,那么请继续看. 如果在看这文章还不知道什么是 UWP 和 W ...

  7. HSSFWorkBooK用法 ---Excel表的导出和设置

    public ActionResult excelPrint() { HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件 HSSFShe ...

  8. TCP/IP协议族(一)

    TCP/IP协议族(一) HTTP简介.请求方法与响应状态码 接下来想系统的回顾一下TCP/IP协议族的相关东西,当然这些东西大部分是在大学的时候学过的,但是那句话,基础的东西还是要不时的回顾回顾的. ...

  9. Mybatis 入门到理解篇

    MyBatis         MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code, ...

  10. WPF 控制程序只能启动一次

    原文:WPF 控制程序只能启动一次 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/jsyhello/article/details/7411898 ...