【问题标题】:Image<Bgr, byte> Always returning NullImage<Bgr, byte> 总是返回 Null
【发布时间】:2016-01-08 03:15:28
【问题描述】:

我正在检查图像文件的大小,如果文件很大,我会重新调整大小,以便处理更快。但是当我使用调整大小代码时,我总是得到一个指向image的空引用异常。我尝试调试->返回的调整大小的位图不是空的,而是转换后的图像> image = new Image( x); 为空。如果我删除调整大小功能,代码工作正常。

Image<Bgr, byte> image = null;

foreach (string s in mylist)
{
    Bitmap x = new Bitmap(Bitmap.FromFile(s));
    if (x.Width > 1000 || x.Height > 1000)
    {
        x = ResizekeepAspectRatio(x, 1000, 1000);
        image = new Image<Bgr, byte>(x);

    }
    else
    {
        image = new Image<Bgr, byte>(x);
    }

    work(image, x, s);
}

----------------------------------------------------------------------

Bitmap ResizekeepAspectRatio(Bitmap imgPhoto, int Width, int Height)
{
    int sourceWidth = imgPhoto.Width;
    int sourceHeight = imgPhoto.Height;
    int sourceX = 0;
    int sourceY = 0;
    int destX = 0;
    int destY = 0;

    float nPercent = 0;
    float nPercentW = 0;
    float nPercentH = 0;

    nPercentW = ((float)Width / (float)sourceWidth);
    nPercentH = ((float)Height / (float)sourceHeight);
    if (nPercentH < nPercentW)
    {
        nPercent = nPercentH;
        destX = System.Convert.ToInt16((Width -
                      (sourceWidth * nPercent)) / 2);
    }
    else
    {
        nPercent = nPercentW;
        destY = System.Convert.ToInt16((Height -
                      (sourceHeight * nPercent)) / 2);
    }

    int destWidth = (int)(sourceWidth * nPercent);
    int destHeight = (int)(sourceHeight * nPercent);

    Bitmap bmPhoto = new Bitmap(Width, Height,
                      PixelFormat.Format24bppRgb);
    bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                     imgPhoto.VerticalResolution);

    Graphics grPhoto = Graphics.FromImage(bmPhoto);
    grPhoto.Clear(Color.Red);
    grPhoto.InterpolationMode =
            InterpolationMode.HighQualityBicubic;

    grPhoto.DrawImage(imgPhoto,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
        GraphicsUnit.Pixel);

    grPhoto.Dispose();
    return bmPhoto;
}

【问题讨论】:

    标签: c# .net emgucv gdk


    【解决方案1】:

    我无法复制错误。我传入了一堆图像并将它们保存了出来,但它们都正确保存了——即使是那些重新调整大小的。

    这让我觉得要么是某些特定的图像失败了,要么是 Emgu 的配置问题。

    你能发布一张失败的图片吗?

    【讨论】:

      猜你喜欢
      • 2017-12-12
      • 2012-08-28
      • 1970-01-01
      • 2014-03-04
      • 2016-11-02
      • 2014-05-06
      • 2012-06-05
      • 2014-05-30
      • 2014-02-23
      相关资源
      最近更新 更多