首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何调整SoftwareBitmap的大小?

如何调整SoftwareBitmap的大小?
EN

Stack Overflow用户
提问于 2016-12-20 21:43:23
回答 3查看 3.2K关注 0票数 3

SoftwareBitmap在UWP中是新的。我首先要说的是:

代码语言:javascript
运行
复制
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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-07-29 07:10:51

在这里,我获取一个图像文件并将其缩小为一个100x100 SoftwareBitmap,并将其作为一个ImageSource返回。

既然你已经有了一个SoftwareBitmap,我想你的任务会更容易。但希望这能给你个主意。

我们只需要WritableBitmap,因为它是PixelBuffer,同时初始化新扩展的SoftwareBitmap实例。如果您可以从我们拥有的IBuffer像素数据(像素局部变量)直接创建一个,那么您可以直接将它提供给SoftwareBitmap.CreateCopyFromBuffer()方法。在这种情况下不需要WritableBitmap

下面是代码:

代码语言:javascript
运行
复制
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应用程序的知识!)

票数 1
EN

Stack Overflow用户

发布于 2016-12-21 12:16:32

看起来像拐杖,但它可能解决你的问题:

代码语言:javascript
运行
复制
    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包裹。

票数 3
EN

Stack Overflow用户

发布于 2016-12-21 20:31:29

我用ScaleEffect做了一些尝试,并以下面的扩展方法结束。事实上,这个方法需要做更多的工作,但是也许它会帮助你更进一步。

代码语言:javascript
运行
复制
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);
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41251716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档