可调整大小的WritableBitmap

3
我已经创建了一个Gray16格式的WriteableBitmap。我想将这个WriteableBitmap调整大小到我已知的尺寸,同时保留像素格式(Gray16)。
有人在调整WriteableBitmap大小方面有经验吗?请帮帮我。
我也在互联网上搜索,并找到了http://writeablebitmapex.codeplex.com/,但是出现了程序集引用错误。
请帮帮我。
2个回答

5
您可以使用以下函数来调整可写位图的大小:
WriteableBitmap resize_image(WriteableBitmap img, double scale)
{
    BitmapSource source = img;

    var s = new ScaleTransform(scale, scale);

    var res = new TransformedBitmap(img, s);

    return convert_BitmapSource_to_WriteableBitmap(res);
}

WriteableBitmap convert_BitmapSource_to_WriteableBitmap(BitmapSource source)
{
    // Calculate stride of source
    int stride = source.PixelWidth * (source.Format.BitsPerPixel / 8);

    // Create data array to hold source pixel data
    byte[] data = new byte[stride * source.PixelHeight];

    // Copy source image pixels to the data array
    source.CopyPixels(data, stride, 0);

    // Create WriteableBitmap to copy the pixel data to.      
    WriteableBitmap target = new WriteableBitmap(source.PixelWidth
        , source.PixelHeight, source.DpiX, source.DpiY
        , source.Format, null);

    // Write the pixel data to the WriteableBitmap.
    target.WritePixels(new Int32Rect(0, 0
        , source.PixelWidth, source.PixelHeight)
        , data, stride, 0);

    return target;
}

警告:变量“source”从未被使用。(在resize_image()中) - Thomas Weller

2
我已经编写了一个函数来调整可写位图的大小。我在我的博客上发布了代码:http://harshaprojects.wordpress.com/2010/08/30/resize-writable-bitmap-in-wpf/ 希望这对你有所帮助。
代码:
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using System.IO;
using System.Windows.Interop;
namespace System.Windows.Media.Imaging
{
public static class RewritableBitmap
{
public static WriteableBitmap ResizeWritableBitmap(this WriteableBitmap wBitmap,int reqWidth, int reqHeight)
{
int Stride = wBitmap.PixelWidth * ((wBitmap.Format.BitsPerPixel + 7) / 8);
int NumPixels = Stride * wBitmap.PixelHeight;
ushort[] ArrayOfPixels = new ushort[NumPixels];


wBitmap.CopyPixels(ArrayOfPixels, Stride, 0);

int OriWidth = (int) wBitmap.PixelWidth;
int OriHeight = (int) wBitmap.PixelHeight;

double nXFactor = (double)OriWidth / (double)reqWidth;
double nYFactor = (double)OriHeight / (double)reqHeight;

double fraction_x, fraction_y, one_minus_x, one_minus_y;
int ceil_x, ceil_y, floor_x, floor_y;

ushort pix1, pix2, pix3, pix4;
int nStride = reqWidth * ((wBitmap.Format.BitsPerPixel + 7) / 8);
int nNumPixels = reqWidth * reqHeight;
ushort[] newArrayOfPixels = new ushort[nNumPixels];
/*Core Part*/
            /* Code project article :
Image Processing for Dummies with C# and GDI+ Part 2 - Convolution Filters By Christian Graus</a>

            href=<a href="http://www.codeproject.com/KB/GDI-plus/csharpfilters.aspx"></a>
            */
            for (int y = 0; y < reqHeight; y++)
{
for (int x = 0; x < reqWidth; x++)
{
// Setup
floor_x = (int)Math.Floor(x * nXFactor);
floor_y = (int)Math.Floor(y * nYFactor);

ceil_x = floor_x + 1;
if (ceil_x >= OriWidth) ceil_x = floor_x;

ceil_y = floor_y + 1;
if (ceil_y >= OriHeight) ceil_y = floor_y;

fraction_x = x * nXFactor - floor_x;
fraction_y = y * nYFactor - floor_y;

one_minus_x = 1.0 - fraction_x;
one_minus_y = 1.0 - fraction_y;

pix1 = ArrayOfPixels[floor_x + floor_y * OriWidth];
pix2 = ArrayOfPixels[ceil_x + floor_y * OriWidth];
pix3 = ArrayOfPixels[floor_x + ceil_y * OriWidth];
pix4 = ArrayOfPixels[ceil_x + ceil_y * OriWidth];

ushort g1 = (ushort)(one_minus_x * pix1 + fraction_x * pix2);
ushort g2 = (ushort)(one_minus_x * pix3 + fraction_x * pix4);
ushort g = (ushort)(one_minus_y * (double)(g1) + fraction_y * (double)(g2));
newArrayOfPixels[y * reqWidth + x] = g;
}
}
           /*End of Core Part*/
WriteableBitmap newWBitmap = new WriteableBitmap(reqWidth, reqHeight, 96, 96, PixelFormats.Gray16, null);
Int32Rect Imagerect = new Int32Rect(0, 0, reqWidth, reqHeight);
int newStride = reqWidth * ((PixelFormats.Gray16.BitsPerPixel + 7) / 8);
newWBitmap.WritePixels(Imagerect, newArrayOfPixels, newStride, 0);
return newWBitmap;
}

}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接