【问题标题】:Edge of the image is cutted out when conver DIBto ImageSource将 DO 转换为 ImageSource 时,图像边缘被切掉
【发布时间】:2016-06-29 10:28:39
【问题描述】:

我使用下面的代码将带有TWAIN 的扫描仪中的DIB 转换为BitmapSource,但我发现图像的边缘被切断了。图像大小与源图像不同。处理图像时,下面的代码有什么问题吗?

/// <summary>
/// Get managed BitmapSource from a DIB provided as a low level windows hadle 
///
/// Notes:
/// Data is copied from the source so the windows handle can be saftely discarded
/// even when the BitmapSource is in use.
/// 
/// Only a subset of possible DIB forrmats is supported.
/// 
/// </summary>
/// <param name="dibHandle"></param>
/// <returns>A copy of the image in a managed BitmapSource </returns>
/// 
public static BitmapSource FormHDib(IntPtr dibHandle)
{
    BitmapSource bs = null;
    IntPtr bmpPtr = IntPtr.Zero;
    bool flip = true; // vertivcally flip the image

    try {
        bmpPtr = Win32.GlobalLock(dibHandle);
        Win32.BITMAPINFOHEADER bmi = new Win32.BITMAPINFOHEADER();
        Marshal.PtrToStructure(bmpPtr, bmi);

        if (bmi.biSizeImage == 0)
            bmi.biSizeImage = (uint)(((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight);

        int palettSize = 0;

        if (bmi.biClrUsed != 0)
            throw new NotSupportedException("DibToBitmap: DIB with pallet is not supported");

        // pointer to the beginning of the bitmap bits
        IntPtr pixptr = (IntPtr)((int)bmpPtr + bmi.biSize + palettSize);

        // Define parameters used to create the BitmapSource.
        PixelFormat pf = PixelFormats.Default;
        switch (bmi.biBitCount) {
            case 32:
                pf = PixelFormats.Bgr32;
                break;
            case 24:
                pf = PixelFormats.Bgr24;
                break;
            case 8:
                pf = PixelFormats.Gray8;
                break;
            case 1:
                pf = PixelFormats.BlackWhite;
                break;
            default:   // not supported
                throw new NotSupportedException("DibToBitmap: Can't determine picture format (biBitCount=" + bmi.biBitCount + ")");
            // break;
        }
        int width = bmi.biWidth;
        int height = bmi.biHeight;
        int stride = (int)(bmi.biSizeImage / height);
        byte[] imageBytes = new byte[stride * height];

        //Debug: Initialize the image with random data.
        //Random value = new Random();
        //value.NextBytes(rawImage);

        if (flip) {
            for (int i = 0, j = 0, k = (height - 1) * stride; i < height; i++, j += stride, k -= stride)
                Marshal.Copy(((IntPtr)((int)pixptr + j)), imageBytes, k, stride);
        } else {
            Marshal.Copy(pixptr, imageBytes, 0, imageBytes.Length);
        }

        int xDpi = (int)Math.Round(bmi.biXPelsPerMeter * 2.54 / 100); // pels per meter to dots per inch
        int yDpi = (int)Math.Round(bmi.biYPelsPerMeter * 2.54 / 100);

        // Create a BitmapSource.
        bs = BitmapSource.Create(width, height, xDpi, yDpi, pf, null, imageBytes, stride);
        Win32.GlobalUnlock(pixptr);
        Win32.GlobalFree(pixptr);
        pixptr = IntPtr.Zero;
        imageBytes = null;
        bmi = null;
    } catch (Exception ex) {
        //string msg = ex.Message;
    }
    finally {
        // cleanup
        if (bmpPtr != IntPtr.Zero) { // locked sucsessfully
            Win32.GlobalUnlock(dibHandle);
        }
    }
    Win32.GlobalUnlock(bmpPtr);
    Win32.GlobalFree(bmpPtr);
    bmpPtr = IntPtr.Zero;

    return bs;
}

此代码来自here

【问题讨论】:

    标签: c# c++ image twain


    【解决方案1】:

    我现在找到了根本原因,它是由错误的TWAIN 设置引起的。 将Edge padding 设置为 none 将解决此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 2019-09-25
      • 2019-07-19
      • 2018-12-27
      • 2018-11-08
      • 2013-09-30
      相关资源
      最近更新 更多