UWP 图片分割,剪切 WriteableBitmap 操作
//WriteableBitmap 转 颜色数组
public static Color[,] ArraytoBitmap(WriteableBitmap writeableBitmap)
{
//《图转化成流》
/////////////////////////////////////////////////////////////////////////////
int width = writeableBitmap.PixelWidth; //获取宽度
int height = writeableBitmap.PixelHeight;//获取高度
Stream stream = writeableBitmap.PixelBuffer.AsStream();//Bitmap=>Stream
byte[] buffer = new byte[stream.Length]; //创建新Buffer,长度是Stream的长度
stream.Read(buffer, 0, buffer.Length); //Stream.Read写入,由Buffer引用(从零到其长度)
//《流转化成组》
/////////////////////////////////////////////////////////////////////////////
Color[,] color = new Color[height, width]; //声明颜色二维数组,用来存储图片
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int Num = (y * width + x) * 4;//计算第一个点的位置
color[y, x] = Color.FromArgb(buffer[Num + 3], buffer[Num + 2], buffer[Num + 1], buffer[Num]);//改变颜色,buffer=>color[,]
}
}
return color; //返回颜色二维数组
}
//颜色数组转WriteableBitmap:
public static WriteableBitmap BitmaptoArray(Color[,] color)
{
//《组转化成流》
//////////////////////////////////////////////////////////////////////////////////////////////////
int height = color.GetLength(0); //获取二维数组的2 { 1,1,1,1,1 }
int width = color.GetLength(1); //获取二维数组的5 { 1,1,1,1,1 }
Byte[] buffer = new Byte[color.Length * 4]; //创建流,长度是数组长度的4倍,存储BGRA四个分量
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Color cooo = color[y, x]; //临时颜色
int Num = (y * width + x) * 4;//计算第一个点的位置
buffer[Num] = cooo.B;//蓝色B
buffer[Num + 1] = cooo.G;//绿色G
buffer[Num + 2] = cooo.R;//红色R
buffer[Num + 3] = cooo.A;//透明度Alpha
}
}
//《流转化成图》
//////////////////////////////////////////////////////////////////////////////////////////////////
WriteableBitmap writeableBitmap = new WriteableBitmap(width, height);//创建新Bitmap,宽高是图片宽高
Stream stream = writeableBitmap.PixelBuffer.AsStream(); //Bitmap=>Stream
stream.Seek(0, SeekOrigin.Begin);//Stream.Seek寻找(从零开始)
stream.Write(buffer, 0, buffer.Length);//Stream.Write写入,写入Buffer(从零到其长度)
return writeableBitmap;
}
待测试。。。。。。
下面这最有效:
public async void BitmapTransformAndSaveTest() { var uncroppedfile = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("uncropped.jpg"); if (uncroppedfile == null) { return; } WriteableBitmap wb = null; // create a stream from the file and decode the image var fileStream = await uncroppedfile.OpenAsync(Windows.Storage.FileAccessMode.Read); var decoder = await BitmapDecoder.CreateAsync(fileStream); // create a new stream and encoder for the new image using (InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream()) { var enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder); // convert the entire bitmap to a 100px by 100px bitmap enc.BitmapTransform.ScaledHeight = 100; enc.BitmapTransform.ScaledWidth = 100; var bounds = new BitmapBounds(); bounds.Height = 50; bounds.Width = 50; bounds.X = 50; bounds.Y = 50; enc.BitmapTransform.Bounds = bounds; // write out to the stream await enc.FlushAsync(); // create a writeable bitmap from the stream ras.Seek(0); wb = new WriteableBitmap(100, 100); wb.SetSource(ras); } // save the cropped file now var croppedfile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("cropped.jpg", Windows.Storage.CreationCollisionOption.ReplaceExisting); using (var stream = await croppedfile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite)) { var pixelStream = wb.PixelBuffer.AsStream(); var bytes = new byte[pixelStream.Length]; pixelStream.Seek(0, SeekOrigin.Begin); await pixelStream.ReadAsync(bytes, 0, (int)pixelStream.Length); await pixelStream.FlushAsync(); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)wb.PixelWidth, (uint)wb.PixelHeight, 0, 0, bytes); await encoder.FlushAsync(); await stream.FlushAsync(); } }
Example of how to scale and crop taken from here:
async private void BitmapTransformTest() { // hard coded image location string filePath = "C:\\Users\\Public\\Pictures\\Sample Pictures\\fantasy-dragons-wallpaper.jpg"; StorageFile file = await StorageFile.GetFileFromPathAsync(filePath); if (file == null) return; // 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); // create a new stream and encoder for the new image InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream(); BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder); // convert the entire bitmap to a 100px by 100px bitmap enc.BitmapTransform.ScaledHeight = 100; enc.BitmapTransform.ScaledWidth = 100; BitmapBounds bounds = new BitmapBounds(); bounds.Height = 50; bounds.Width = 50; bounds.X = 50; bounds.Y = 50; enc.BitmapTransform.Bounds = bounds; // write out to the stream try { await enc.FlushAsync(); } catch (Exception ex) { string s = ex.ToString(); } // render the stream to the screen BitmapImage bImg = new BitmapImage(); bImg.SetSource(ras); img.Source = bImg; // image element in xaml }
相关文章:https://stackoverflow.com/questions/12349611/how-to-resize-image-in-c-sharp-winrt-winmd
http://blog.csdn.net/lindexi_gd/article/details/54612553
fffffffffffffffff
test red font.