C# 图像<;Bgr,字节>;总是返回空值
我正在检查图像文件的大小,如果文件很大,我会重新调整大小,以便处理速度更快。但是当我使用调整大小代码时,我总是得到一个指向C# 图像<;Bgr,字节>;总是返回空值,c#,.net,emgucv,gdk,C#,.net,Emgucv,Gdk,我正在检查图像文件的大小,如果文件很大,我会重新调整大小,以便处理速度更快。但是当我使用调整大小代码时,我总是得到一个指向图像的空引用异常。我尝试调试->返回的调整大小的位图不是空的,而是转换后的图像>图像=新图像(x)为空。如果我删除resize函数,代码可以正常工作 Image<Bgr, byte> image = null; foreach (string s in mylist) { Bitmap x = new Bitmap(Bitmap.FromFile(s))
图像的空引用异常。我尝试调试->返回的调整大小的位图不是空的,而是转换后的图像>图像=新图像(x)为空。如果我删除resize函数,代码可以正常工作
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;
}
Image=null;
foreach(mylist中的字符串s)
{
位图x=新位图(Bitmap.FromFile);
如果(x.宽度>1000 | | x.高度>1000)
{
x=尺寸基波谱比(x,1000,1000);
图像=新图像(x);
}
其他的
{
图像=新图像(x);
}
作品(图像,x,s);
}
----------------------------------------------------------------------
位图大小keepasspectratio(位图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;
浮动百分比=0;
浮动nPercentW=0;
浮点数nPercentH=0;
nPercentW=((浮点)宽度/(浮点)源宽度);
nPercentH=((浮动)高度/(浮动)源高度);
如果(nPercentH
复制错误时遇到问题。我传入了一组图像并保存了下来,但它们都保存正确——即使是那些重新调整大小的图像
这让我觉得要么是某些特定的映像失败了,要么可能是Emgu的配置问题
你能发布一个失败的图片吗