图像处理 - 自动调整大小/裁剪缩略图

时间:2012-02-14 20:41:08

标签: c# silverlight image image-processing

我用这个打了一堵砖墙...我知道我的缺陷在某种程度上是在逻辑上,但我会解释我在哪里。

我(尝试)编写代码,为Silverlight应用程序底部的导航栏动态创建一个方形(75x75)像素缩略图。 当我在下面调试我的代码时,我在扩展嵌套循环期间不断收到“数组索引超出范围”错误 (我只使用srcWidth> srcHeight调试,一次一步)

我正在测试的源图像是307x162(49734像素) 目标大小(每个当前逻辑)为150x75(11250像素) 当然,在此之后,我打算将结果裁剪为75x75

在将11251作为目的地索引之前,只有大约一半的源图像。 我知道这是我的逻辑, 我只是不知道我用来构建目的地的大小是否出错, 或者用于将浮子浇铸到铸件中的近似值, 或者它可能都不是

我不知道,这就是我在这里发帖的原因......无论如何,这是我的来源:

    private void ResizeNavBarImage(Image src)
    {
        const int SizeOfRGB = 4;
        WriteableBitmap bmpSource = new WriteableBitmap((BitmapSource)src.Source);
        int[] iSourcePixels = bmpSource.Pixels;
        int srcWidth = bmpSource.PixelWidth;
        int srcHeight = bmpSource.PixelHeight;

        int destWidth = 75;
        int destHeight = 75;

        float xFactor = srcWidth / destWidth;
        float yFactor = srcHeight / destHeight;

        float xSource, ySource;
        int iApprox;
        int iIndex = 0;

        if (srcWidth > srcHeight)
        {
            WriteableBitmap bmpDest = new WriteableBitmap((int)(destWidth * yFactor), destHeight);
            int[] iDestPixels = bmpDest.Pixels;

            // Resize srcHeight to destHeight, srcWidth to same ratio
            for (int i = 0; i < srcHeight; i++)
            {
                for (int j = 0; j < srcWidth; j++)
                {
                    xSource = j * yFactor;
                    ySource = i * yFactor;
                    iApprox = (int)(ySource * srcWidth + xSource);
                    iDestPixels[iIndex++] = iSourcePixels[iApprox];
                }
            }

            // Crop half of difference from each side of the width
            srcWidth = bmpDest.PixelWidth;
            srcHeight = bmpDest.PixelHeight;
            int xLeftOffset = (srcWidth - srcHeight) / 2;

            WriteableBitmap bmpFinalDest = new WriteableBitmap(destWidth, destHeight);
            for (int iPixelRow = 0; iPixelRow < destHeight; iPixelRow++)
            {
                int srcOffset = (iPixelRow * srcWidth + xLeftOffset) * SizeOfRGB;
                int destOffset = iPixelRow * destWidth * SizeOfRGB;
                Buffer.BlockCopy(bmpDest.Pixels, srcOffset, bmpFinalDest.Pixels, destOffset, destWidth * SizeOfRGB);
            }

            src.Source = (ImageSource)bmpFinalDest;
        }
        else if (srcWidth < srcHeight)
        {
            WriteableBitmap bmpDest = new WriteableBitmap(destWidth, (int)(destHeight * xFactor));
            int[] iDestPixels = bmpDest.Pixels;

            // Resize srcWidth to destWidth, srcHeight to same ratio
            for (int i = 0; i < srcHeight; i++)
            {
                for (int j = 0; j < srcWidth; j++)
                {
                    xSource = j * xFactor;
                    ySource = i * xFactor;
                    iApprox = (int)(ySource * srcWidth + xSource);
                    iDestPixels[iIndex++] = iSourcePixels[iApprox];
                }
            }

            // Crop half of difference from each side of the height
            srcWidth = bmpDest.PixelWidth;
            srcHeight = bmpDest.PixelHeight;
            int yTopOffset = (srcHeight - srcWidth) / 2;

            WriteableBitmap bmpFinalDest = new WriteableBitmap(destWidth, destHeight);
            for (int iPixelRow = yTopOffset; iPixelRow < (destHeight - (yTopOffset * 2)); iPixelRow++)
            {
                int srcOffset = iPixelRow * srcWidth * SizeOfRGB;
                int destOffset = iPixelRow * destWidth * SizeOfRGB;
                Buffer.BlockCopy(bmpDest.Pixels, srcOffset, bmpFinalDest.Pixels, destOffset, destWidth * SizeOfRGB);
            }

            src.Source = (ImageSource)bmpFinalDest;
        }
        else // (srcWidth == srcHeight)
        {
            WriteableBitmap bmpDest = new WriteableBitmap(destWidth, destHeight);
            int[] iDestPixels = bmpDest.Pixels;

            // Resize and set source
            for (var i = 0; i < srcHeight; i++)
            {
                for (var j = 0; j < srcWidth; j++)
                {
                    xSource = j * xFactor;
                    ySource = i * yFactor;
                    iApprox = (int)(ySource * srcWidth + xSource);
                    iDestPixels[iIndex++] = iSourcePixels[iApprox];
                }
            }
            src.Source = (ImageSource)bmpDest;
        }
    }

=============================================== ================================

这是后代的工作代码(使用WriteableBitmapEx):

    private void ResizeNavBarImage(Image src)
    {
        WriteableBitmap bmpSource = new WriteableBitmap((BitmapSource)src.Source);
        int srcWidth = bmpSource.PixelWidth;
        int srcHeight = bmpSource.PixelHeight;

        int finalDestWidth = 75;
        int finalDestHeight = 75;

        // Resize
        float xFactor = ((float)finalDestWidth / (float)srcWidth);
        float yFactor = ((float)finalDestHeight / (float)srcHeight);
        float Factor = 0;

        if (xFactor < yFactor)
            Factor = yFactor;
        else
            Factor = xFactor;

        int destWidth = (int)(srcWidth * Factor);
        int destHeight = (int)(srcHeight * Factor);

        if (destWidth < destHeight && destWidth != finalDestWidth)
            destWidth = finalDestWidth;
        else if (destWidth > destHeight && destHeight != finalDestHeight)
            destHeight = finalDestHeight;

        WriteableBitmap bmpDest = bmpSource.Resize(destWidth, destHeight, WriteableBitmapExtensions.Interpolation.Bilinear);

        // Crop
        int Offset;
        WriteableBitmap bmpFinalDest = new WriteableBitmap(finalDestWidth, finalDestHeight);

        if (destWidth > destHeight)
        {
            Offset = (bmpDest.PixelWidth - bmpDest.PixelHeight) / 2;
            if (finalDestWidth % 2 != 0 && Offset % 2 == 0)
                Offset -= 1;
            bmpFinalDest = bmpDest.Crop(Offset, 0, finalDestWidth, finalDestHeight);
        }
        else if (destWidth < destHeight)
        {
            Offset = (bmpDest.PixelHeight - bmpDest.PixelWidth) / 2;
            if (finalDestHeight % 2 != 0 && Offset % 2 == 0)
                Offset -= 1;
            bmpFinalDest = bmpDest.Crop(0, Offset, finalDestWidth, finalDestHeight);
        }
        else
            bmpFinalDest = bmpDest;

        src.Source = (ImageSource)bmpFinalDest;
    }

3 个答案:

答案 0 :(得分:1)

ij的循环将转到srcHeightsrcWidth,而不是destHeightdestWidth

这可能不是此代码中的最后一个错误。

答案 1 :(得分:0)

我同意Mark Ransom,调整图像大小的正确方法是使用Graphics对象绘制到另一个图像。

http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing

这将允许您使用不同的,更好的滤镜来获得良好的图像质量,更不用说速度了。

答案 2 :(得分:0)

为什么不使用WriteableBitmap的内置缩放功能?

WriteableBitmap wb = new WriteableBitmap(src, new ScaleTransform() { ScaleX = 0.25, ScaleY = 0.25 });
wb.Invalidate();
src.Source = wb;