博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Metro style app 图片Scale ,Crop. 图片的打开,保存
阅读量:7167 次
发布时间:2019-06-29

本文共 5105 字,大约阅读时间需要 17 分钟。

Metro style app

一.图片Scale ,Crop操作

public 
class 
PhotoEdit
{
    
public 
async
static 
Task<WriteableBitmap> ScaleAndCorpPhoto(IStorageFile file)
    
{
        
if 
(file ==
null
)
return 
null
;
        
// create a stream from the file and decode the image
        
var 
fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
        
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
        
BitmapTransform transform =
new 
BitmapTransform();
        
BitmapBounds bounds =
new 
BitmapBounds();
        
transform.ScaledWidth = 500;
        
transform.ScaledHeight = 500;
        
const 
int 
croppedHeight = 400;
        
const 
int 
croppedWidth = 400;
        
bounds.Height = croppedHeight;
        
bounds.Width = croppedWidth;
        
bounds.X = 0;
        
bounds.Y = 0;
        
transform.Bounds = bounds;
 
        
// Get the pixels in Bgra8 to match what the WriteableBitmap will expect
        
PixelDataProvider pixelData = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb);
        
byte
[] pixels = pixelData.DetachPixelData();
 
        
// And stream them into a WriteableBitmap
        
WriteableBitmap cropBmp =
new 
WriteableBitmap(croppedHeight, croppedWidth);
        
Stream pixStream = cropBmp.PixelBuffer.AsStream();
        
pixStream.Write(pixels, 0, pixels.Length);
        
return 
cropBmp;
    
}
}

 调用方法

private 
async System.Threading.Tasks.Task ScaleAndCorpPhoto()
{
    
FileOpenPicker fileOpenPicker =
new 
FileOpenPicker();
    
fileOpenPicker.CommitButtonText =
"打开"
;
    
fileOpenPicker.FileTypeFilter.Insert(0,
".jpg"
);
    
StorageFile file = await fileOpenPicker.PickSingleFileAsync();
    
if 
(file ==
null
)
return
;
    
image.Source = await PhotoEdit.ScaleAndCorpPhoto(file);
}

 二、图片的打开与保存

1.图片的打开. 选择要显示的图片,最后将图片显示在Image控件中.

private 
static 
BitmapImage srcImage =
new 
BitmapImage();
 
private 
static 
WriteableBitmap wbsrcImage;
 
public 
async
static 
void 
OpenImage(Image ImageOne)
 
{
     
FileOpenPicker imagePicker =
new 
FileOpenPicker
     
{
         
ViewMode = PickerViewMode.Thumbnail,
         
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
         
FileTypeFilter = {
".jpg"
,
".jpeg"
,
".png"
,
".bmp" 
}
     
};
 
     
Guid decoderId;
     
StorageFile imageFile = await imagePicker.PickSingleFileAsync();
     
if 
(imageFile !=
null
)
     
{
         
srcImage =
new 
BitmapImage();
         
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 =
new 
WriteableBitmap(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.图片的保存. 将获得的WriteableBitmap保存起来. 比如你可以将Scale和Crop的图片保存起来.

public 
async
static 
Task SaveImage(WriteableBitmap src)
{
    
FileSavePicker save =
new 
FileSavePicker();
    
save.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    
save.DefaultFileExtension =
".jpg"
;
    
save.SuggestedFileName =
"newimage"
;
    
save.FileTypeChoices.Add(
".bmp"
,
new 
List<
string
>() {
".bmp" 
});
    
save.FileTypeChoices.Add(
".png"
,
new 
List<
string
>() {
".png" 
});
    
save.FileTypeChoices.Add(
".jpg"
,
new 
List<
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 = await BitmapEncoder.CreateAsync(encoderId, fileStream);
        
Stream pixelStream = src.PixelBuffer.AsStream();
        
byte
[] pixels =
new 
byte
[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;
    
}
}

 

本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2012/11/28/2792726.html,如需转载请自行联系原作者

你可能感兴趣的文章
调浏览器兼容时遇到的问题
查看>>
java中的并发:进程和线程
查看>>
我的友情链接
查看>>
JAVAEE项目结构以及并发随想
查看>>
iOS开发之生成图片水印
查看>>
PJSIP 网络视频,电话
查看>>
详解spring 每个jar的作用
查看>>
关于云的日记
查看>>
linux安装
查看>>
利用dbms_repair来标记和跳过坏块
查看>>
解决ssh免密码登录 非默认端口22免密钥登录
查看>>
HTTP错误码解释(状态码)
查看>>
ubuntu解压命令
查看>>
python3.x的print()函数默认参数
查看>>
嵌入式 Linux进程间通信(二)——exec族函数
查看>>
Oracle数据库精讲课程之Rac管理最全课程
查看>>
我的友情链接
查看>>
Spring学习总结(4)——Spring AOP教程
查看>>
Jenkins自动化构建(一)
查看>>
log4j配置详解
查看>>