Metro style app 图片Scale ,Crop. 图片的打开,保存

简介:

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,如需转载请自行联系原作者


目录
相关文章
|
8月前
|
XML Java Android开发
Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)
Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)
167 0
|
8月前
|
XML Java Android开发
Android Studio App开发之使用相机拍摄照片和从相册中选取图片(附源码 超详细必看)
Android Studio App开发之使用相机拍摄照片和从相册中选取图片(附源码 超详细必看)
1017 0
|
3月前
|
小程序 JavaScript API
微信小程序开发之:保存图片到手机,使用uni-app 开发小程序;还有微信原生保存图片到手机
这篇文章介绍了如何在uni-app和微信小程序中实现将图片保存到用户手机相册的功能。
1290 0
微信小程序开发之:保存图片到手机,使用uni-app 开发小程序;还有微信原生保存图片到手机
|
3月前
|
前端开发 UED 开发者
uni-app:去除导航栏&跨域的问题&blobe查看图片&v-deep&页面操作 (五)
本文介绍了几个前端开发技巧:1) 如何通过设置 `navigationStyle` 为 `custom` 去除顶部导航;2) 解决跨域问题的方法,包括使用 `dotenv` 加载全局变量和配置 `devServer` 的代理;3) 使用 Blob 和 FileReader 查看图片;4) 利用 `v-deep` 深度作用选择器修改样式;5) 修改页面左上角返回按钮的行为。
|
8月前
|
移动开发
uni-app使用v-html输出富文本图片溢出解决
uni-app使用v-html输出富文本图片溢出解决
729 1
|
5月前
|
Java Go Windows
【应用服务 App Service】App Service中上传文件/图片(> 2M)后就出现500错误(Maximum request length exceeded).
【应用服务 App Service】App Service中上传文件/图片(> 2M)后就出现500错误(Maximum request length exceeded).
图库,设计类软件,App视频截图软件,外加设计图库,在你截取视频就能够实现图片收录,通过设计类网站后台控制系统,可以提前设置好,统计的分类内容,定义好分类,自动收录图片,再将截图汇总整理展示
图库,设计类软件,App视频截图软件,外加设计图库,在你截取视频就能够实现图片收录,通过设计类网站后台控制系统,可以提前设置好,统计的分类内容,定义好分类,自动收录图片,再将截图汇总整理展示
图库,设计类软件,App视频截图软件,外加设计图库,在你截取视频就能够实现图片收录,通过设计类网站后台控制系统,可以提前设置好,统计的分类内容,定义好分类,自动收录图片,再将截图汇总整理展示
|
8月前
|
XML JSON Java
Android App开发即时通信中通过SocketIO在客户端与服务端间传输文本和图片的讲解及实战(超详细 附源码)
Android App开发即时通信中通过SocketIO在客户端与服务端间传输文本和图片的讲解及实战(超详细 附源码)
482 0
|
8月前
uni-app 157发布朋友圈-批量上传图片
uni-app 157发布朋友圈-批量上传图片
69 0
|
8月前
uni-app 114发送图片功能
uni-app 114发送图片功能
51 0