通过裁剪获取新图像
Posted
技术标签:
【中文标题】通过裁剪获取新图像【英文标题】:Get a new Image by cropping 【发布时间】:2017-10-13 14:55:15 【问题描述】:我正在尝试将裁剪后的图像(和/或网格)转换为新的普通图像,但它在裁剪后返回,在相同的位置,如何修复它,裁剪后我的程序如何工作
private async void Save(object sender, RoutedEventArgs e)
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(GGrid, 4096 , 2448);
// IBuffer pixels = await renderTargetBitmap.GetPixelsAsync();
IBuffer pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
FileSavePicker fileSavePicker = new FileSavePicker();
fileSavePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
fileSavePicker.FileTypeChoices.Add("JPEG files", new List<string>() ".png");
fileSavePicker.SuggestedFileName = "image";
var outputFile = await fileSavePicker.PickSaveFileAsync();
if (outputFile == null)
// The user cancelled the picking operation
return;
using (IRandomAccessStream stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,
DisplayInformation.GetForCurrentView().LogicalDpi,
DisplayInformation.GetForCurrentView().LogicalDpi,
pixelBuffer.ToArray());
try
await encoder.FlushAsync();
catch (Exception err)
switch (err.HResult)
case unchecked((int)0x88982F81): //WINCODEC_ERR_UNSUPPORTEDOPERATION
// If the encoder does not support writing a thumbnail, then try again
// but disable thumbnail generation.
encoder.IsThumbnailGenerated = false;
break;
default:
throw err;
//if (encoder.IsThumbnailGenerated == false)
//
// await encoder.FlushAsync();
//
【问题讨论】:
GGrid
的内容是什么样的?你能发布 XAML 和屏幕截图吗?
我已经弄好了,谢谢,我只是改XAML
【参考方案1】:
您需要翻译和裁剪位图。
double x,y,w,h;
GGrid.RenderTransform = new TranslateTransform
X = -x, // translate back to origin (0,0)
Y = -y
;
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(GGrid, w, h); // use crop size
【讨论】:
感谢回复,实际上它并没有创建新的图像只是改变了位置,还有空白空间 当您使用较小的尺寸时会发生什么?await renderTargetBitmap.RenderAsync(GGrid, 400 , 300);
你看到的黑色空间更少了吗?
我看到更多的黑色空间,它只是让图像更小以上是关于通过裁剪获取新图像的主要内容,如果未能解决你的问题,请参考以下文章