首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Image<Bgr,byte>总是返回空

Image<Bgr,byte>总是返回空
EN

Stack Overflow用户
提问于 2016-01-07 11:23:50
回答 1查看 312关注 0票数 0

我检查图像文件的大小,如果文件是大的,那么我调整它的大小,以使处理更快。但是,当我使用调整大小的代码时,我总是得到一个空引用异常,指向尝试调试的image.I ->返回的调整大小的位图不是null,但是转换后的图像>图像=新图像(X);如果删除调整大小函数, is null.The代码工作得很好。

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2016-01-07 19:15:28

我在复制错误时遇到了麻烦。我传递了一堆图像并把它们保存了出来,但是它们都是正确保存的--甚至是那些调整大小的图像。

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34653907

复制
相关文章

相似问题

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