SoftwareBitmap
在UWP中是新的。我首先要说的是:
var softwareBitmap = EXTERNALVALUE;
// do I even need a writeable bitmap?
var writeableBitmap = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight);
softwareBitmap.CopyToBuffer(writeableBitmap.PixelBuffer);
// maybe use BitmapDecoder?
我不知所措。谢谢。
请注意,我不是指BitmapImage
;我指的是SoftwareBitmap
。
发布于 2017-07-29 07:10:51
在这里,我获取一个图像文件并将其缩小为一个100x100 SoftwareBitmap
,并将其作为一个ImageSource
返回。
既然你已经有了一个SoftwareBitmap
,我想你的任务会更容易。但希望这能给你个主意。
我们只需要WritableBitmap
,因为它是PixelBuffer
,同时初始化新扩展的SoftwareBitmap
实例。如果您可以从我们拥有的IBuffer像素数据(像素局部变量)直接创建一个,那么您可以直接将它提供给SoftwareBitmap.CreateCopyFromBuffer()
方法。在这种情况下不需要WritableBitmap
。
下面是代码:
private async Task<ImageSource> ProcessImageAsync(StorageFile ImageFile)
{
if (ImageFile == null)
throw new ArgumentNullException("ImageFile cannot be null.");
//The new size of processed image.
const int side = 100;
//Initialize bitmap transformations to be applied to the image.
var transform = new BitmapTransform() { ScaledWidth = side, ScaledHeight = side, InterpolationMode = BitmapInterpolationMode.Cubic };
//Get image pixels.
var stream = await ImageFile.OpenStreamForReadAsync();
var decoder = await BitmapDecoder.CreateAsync(stream.AsRandomAccessStream());
var pixelData = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb);
var pixels = pixelData.DetachPixelData();
//Initialize writable bitmap.
var wBitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
await wBitmap.SetSourceAsync(stream.AsRandomAccessStream());
//Create a software bitmap from the writable bitmap's pixel buffer.
var sBitmap = SoftwareBitmap.CreateCopyFromBuffer(wBitmap.PixelBuffer, BitmapPixelFormat.Bgra8, side, side, BitmapAlphaMode.Premultiplied);
//Create software bitmap source.
var sBitmapSource = new SoftwareBitmapSource();
await sBitmapSource.SetBitmapAsync(sBitmap);
return sBitmapSource;
}
PS。我知道这句话不应该是答案的一部分,但我必须说,我已经从你的MVA和Channel9视频中学到了很多关于XAML/C#和开发Windows应用程序的知识!)
发布于 2016-12-21 12:16:32
看起来像拐杖,但它可能解决你的问题:
private async Task<SoftwareBitmap> ResizeSoftwareBitmap(SoftwareBitmap softwareBitmap, double scaleFactor)
{
var resourceCreator = CanvasDevice.GetSharedDevice();
var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(resourceCreator, softwareBitmap);
var canvasRenderTarget = new CanvasRenderTarget(resourceCreator, (int)(softwareBitmap.PixelWidth * scaleFactor), (int)(softwareBitmap.PixelHeight * scaleFactor), 96);
using (var cds = canvasRenderTarget.CreateDrawingSession())
{
cds.DrawImage(canvasBitmap, canvasRenderTarget.Bounds);
}
var pixelBytes = canvasRenderTarget.GetPixelBytes();
var writeableBitmap = new WriteableBitmap((int)(softwareBitmap.PixelWidth * scaleFactor), (int)(softwareBitmap.PixelHeight * scaleFactor));
using (var stream = writeableBitmap.PixelBuffer.AsStream())
{
await stream.WriteAsync(pixelBytes, 0, pixelBytes.Length);
}
var scaledSoftwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, (int)(softwareBitmap.PixelWidth * scaleFactor), (int)(softwareBitmap.PixelHeight * scaleFactor));
scaledSoftwareBitmap.CopyFromBuffer(writeableBitmap.PixelBuffer);
return scaledSoftwareBitmap;
}
你必须从nuget那里得到Win2D包裹。
发布于 2016-12-21 20:31:29
我用ScaleEffect做了一些尝试,并以下面的扩展方法结束。事实上,这个方法需要做更多的工作,但是也许它会帮助你更进一步。
public static SoftwareBitmap Resize(this SoftwareBitmap softwareBitmap, float newWidth, float newHeight)
{
using (var resourceCreator = CanvasDevice.GetSharedDevice())
using (var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(resourceCreator, softwareBitmap))
using (var canvasRenderTarget = new CanvasRenderTarget(resourceCreator, newWidth, newHeight, canvasBitmap.Dpi))
using (var drawingSession = canvasRenderTarget.CreateDrawingSession())
using (var scaleEffect = new ScaleEffect())
{
scaleEffect.Source = canvasBitmap;
scaleEffect.Scale = new System.Numerics.Vector2(newWidth / softwareBitmap.PixelWidth, newHeight / softwareBitmap.PixelHeight);
drawingSession.DrawImage(scaleEffect);
drawingSession.Flush();
return SoftwareBitmap.CreateCopyFromBuffer(canvasRenderTarget.GetPixelBytes().AsBuffer(), BitmapPixelFormat.Bgra8, (int)newWidth, (int)newHeight, BitmapAlphaMode.Premultiplied);
}
}
https://stackoverflow.com/questions/41251716
复制相似问题