原文: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. Java中a=a+b 与 a+=b差别

    一般觉得a=a+b的运行效率是低于a+=b的,由于它多进行了一步中间变量的操作,并且会多占用一个变量的空间.而Java编译器默认对其进行了优化,优化之后两条语句都当做 a+=b来运行了,所以实际上是没 ...

  2. C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。

    C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结. 一.总结 C++/Php/Python/Shell 程序按行读取文件或者控制台(php读取标准输入:$fp = fope ...

  3. [Postgres] Filter Data in a Postgres Table with Query Statements

    We have all this data, but how do we answer questions about it? In this lesson we’ll learn how to fi ...

  4. 安全配置基线Linux系统

    Linux系统安全配置基线 一:共享账号检查 配置名称:用户账号分配检查,避免共享账号存在 配置要求:1.系统需按照实际用户分配账号: 2.避免不同用户间共享账号,避免用户账号和服务器间通信使用的账号 ...

  5. [Ramda] Refactor to a Point Free Function with Ramda's useWith Function

    Naming things is hard and arguments in generic utility functions are no exception. Making functions ...

  6. js中的对象与数组

    js对象与数组是js中最基本的概念, 定义对象时可用 var a = {} 定义一个空对象 定义数组时可用 var a = [] 定义一个空字符串.. 在对象中只是存在属性,属性与值之间用" ...

  7. python 爬取豆瓣的美剧

    pc版大概有500条记录,mobile大概是50部,只有热门的,所以少一点 url构造很简单,主要参数就是page_limit与page_start,每翻一页,start+=20即可,tag是&quo ...

  8. 关于Boolean类型做为同步锁异常问题

    public class Test2 { private static volatile Boolean aBoolean = true; static class A implements Runn ...

  9. Operating system coordinated thermal management

    A processor's performance state may be adjusted based on processor temperature. On transitions to a ...

  10. matplotlib油漆基础

    http://blog.csdn.net/pipisorry/article/details/37742423 matplotlib介绍 matplotlib 是python最著名的画图库,它提供了一 ...