Win8 Metro(C#) 数字图像处理--1 图像打开,保存
原文: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 图像打开,保存的更多相关文章
- Win8 Metro(C#)数字图像处理--4图像颜色空间描述
原文:Win8 Metro(C#)数字图像处理--4图像颜色空间描述 图像颜色空间是图像颜色集合的数学表示,本小节将针对几种常见颜色空间做个简单介绍. /// <summary> / ...
- Win8 Metro(C#)数字图像处理--3.2图像方差计算
原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...
- Win8 Metro(C#)数字图像处理--3.3图像直方图计算
原文:Win8 Metro(C#)数字图像处理--3.3图像直方图计算 /// <summary> /// Get the array of histrgram. /// </sum ...
- Win8 Metro(C#)数字图像处理--3.4图像信息熵计算
原文:Win8 Metro(C#)数字图像处理--3.4图像信息熵计算 [函数代码] /// <summary> /// Entropy of one image. /// </su ...
- Win8 Metro(C#)数字图像处理--3.5图像形心计算
原文:Win8 Metro(C#)数字图像处理--3.5图像形心计算 /// <summary> /// Get the center of the object in an image. ...
- Win8 Metro(C#)数字图像处理--2.73一种背景图像融合特效
原文:Win8 Metro(C#)数字图像处理--2.73一种背景图像融合特效 /// <summary> /// Image merge process. /// </summar ...
- Win8 Metro(C#)数字图像处理--3.1图像均值计算
原文:Win8 Metro(C#)数字图像处理--3.1图像均值计算 /// <summary> /// Mean value computing. /// </summary> ...
- Win8 Metro(C#)数字图像处理--2.74图像凸包计算
原文:Win8 Metro(C#)数字图像处理--2.74图像凸包计算 /// <summary> /// Convex Hull compute. /// </summary> ...
- Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器
原文:Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器 /// <summary> /// Min value filter. /// </summary> ...
随机推荐
- C# WebQQ协议群发机器人(一)
原创性申明 本文地址 http://blog.csdn.net/zhujunxxxxx/article/details/38931287 转载的话请注明出处. 之前我也写过一篇使用python来实现的 ...
- [Ramda] Create a Query String from an Object using Ramda's toPairs function
In this lesson, we'll use Ramda's toPairs function, along with map, join, concatand compose to creat ...
- as.data.frame一定要小心的一个參数stringsAsFactors
假设说一个data.frame中的元素是factor.你想转化成numeric,你会怎么做?比方d[1,1]是factor 正确答案是 先as.character(x) 再as.numeric(x ...
- 物联网学生科协第三届H-star现场编程比赛
问题 A: 剪纸片 时间限制: 1 Sec 内存限制: 128 MB 题目描写叙述 这是一道简单的题目,假如你身边有一张纸.一把剪刀.在H-star的比赛现场,你会这么做: 1. 将这张纸剪成两片(平 ...
- xCode中怎样保存自己的代码块
在开发iOS的过程中.xCode肯定是用得最多的工具.没有之中的一个.由于苹果官方提供的就这一个平台,尽管没有竞争对手,但秉承苹果一贯的注重细节的原则,xCode还是一款相当不错的IDE. 作为一名i ...
- html 横线的代码
第一种: <hr style=" height:2px;border:none;border-top:2px dotted #185598;" /> 园点虚线 he ...
- 【b302】侦探推理
Time Limit: 1 second Memory Limit: 50 MB [问题描述] 明明同学最近迷上了侦探漫画<柯南>并沉醉于推理游戏之中,于是他召集了一群同学玩推理游戏.游戏 ...
- js声明json数据,打印json数据,遍历json数据,转换json数据为数组
1.js声明json数据: 2.打印json数据: 3.遍历json数据: 4.转换json数据为数组; //声明JSON var json = {}; json.a = 1; //第一种赋值方式(仿 ...
- POJ 2418-Hardwood Species(map)
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 18770 Accepted: 740 ...
- 【25.00%】【vijos P1907】飞扬的小鸟
描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管或者掉在地上的话,便宣告 ...