Image.save - 参数无效。 [英] Image.save - the parameter is invalid.

查看:736
本文介绍了Image.save - 参数无效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用代表图像的流,并将其绘制到更大的图像上。为此,我为 MemoryStream s编写了一个扩展方法。当我尝试将图像保存到新流时,代码抛出参数无效异常。



事实:

- 我正在调用方法的流是非空的,并且包含数据。

- 当我到达有问题的语句时,新图像不为空,编码器似乎是正确。

- 收到的 MemoryStream 对象是非空的(实际上,这会导致一个完全不同的异常)。



我不知道为什么我得到这个例外。



如你所见,我正在使用使用语句,但是当我到达抛出异常的语句时,没有任何内容超出范围,并且异常并不表示WHICH参数无效。



注 - GetEncoder 方法被写入黑盒并清理使用该代码所需的方法。如上所述,编码器似乎有效。



  public  静态 MemoryStream AddMatting( MemoryStream流,大小,颜色)
{
MemoryStream newStream = new MemoryStream();
使用(图片图片= Bitmap.FromStream(流))
{
使用(位图matting = new 位图(size.Width,size.Height,image.PixelFormat))
{
使用(图形g = Graphics.FromImage(matting))
{
int x =( int )((matting.Width-image.Width)* 0. 5 );
int y =( int )((matting.Height-image.Height)* 0 。 5 );
g.Clear(color);
g.DrawImage(image,x,y);
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
使用(EncoderParameters encParams = new EncoderParameters( 1 ))
{
encParams.Param [ 0 ] = new EncoderParameter(Encoder.Quality, 100 );
尝试
{
matting.Save(newStream,jpgEncoder,encParams);
}
catch (例外情况)
{
if (ex!= null ){}
}
}
}
}
}
return newStream;
}





我的尝试:



0)我将 true,true 添加到 FromStream 的方法调用中,想到可能有些如何流无效,但是没有触发 ArgumentInvalid 异常,所以流本身是有效的。



1)我尝试将方法简化为最简单的形式(它接收传入的流,使其成为图像,然后将其保存回新的流),并且仍然得到例外:



  public   static  MemoryStream AddMatting(< span class =code-keyword>此 MemoryStream流,大小,颜色)
{
MemoryStream newStream = new 的MemoryStream();
使用 var image = Bitmap.FromStream(stream))
{
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
使用(EncoderParameters encoderParameters = new EncoderParameters( 1 ))
{
encoderParameters.Param [ 0 ] = new EncoderParameter(Encoder.Quality, 100 );
尝试
{
image.Save(newStream,jpgEncoder,encoderParameters);
}
catch (例外情况)
{
if (ex!= null ){}
}
}
}
返回 newStream;
}

解决方案

问题是编码器参数:质量必须是长而不是int [ ^ ]。

没关系。我没有溪流,我想要发生的一切都在发生。


I'm starting with a stream that represents an image, and drawing it onto a larger image. To do this, I wrote an extension method for MemoryStreams. When I try to save the image into a new stream, the code is throwing "Parameter is not valid" exception.

Facts:
- The stream on which I'm calling the method is NOT null, and contains data.
- When I get to the statement in question, the new image is not null, and the encoder appears to be correct.
- The recieving MemoryStream object is NOT null (indeed, that would have caused a completely different exception).

I have no clue as to why I'm getting this exception.

As you can see, I'm making use of using statements, but nothing is out of scope when I get to the statement that is throwing the exception, and the exception does not indicate WHICH parameter is invalid.

Note - the GetEncoder method was written to black-box and cleanup the methods that needed to use that code. As stated above, the encoder appears to be valid.

public static MemoryStream AddMatting(this MemoryStream stream, Size size, Color color)
{
    MemoryStream newStream = new MemoryStream();
    using (Image image = Bitmap.FromStream(stream))
    {
        using (Bitmap matting = new Bitmap(size.Width, size.Height, image.PixelFormat))
        {
            using (Graphics g = Graphics.FromImage(matting))
            {
                int x = (int)((matting.Width-image.Width)*0.5);
                int y = (int)((matting.Height-image.Height)*0.5);
                g.Clear(color);
                g.DrawImage(image, x, y);
                ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
                using (EncoderParameters encParams = new EncoderParameters(1))
                {
                    encParams.Param[0] = new EncoderParameter(Encoder.Quality, 100);
                    try
                    {
                        matting.Save(newStream, jpgEncoder, encParams);
                    }
                    catch (Exception ex)
                    {
                        if (ex != null) {}
                    }
                }
            }
        }
    }
    return newStream;
}



What I have tried:

0) I added true,true to the method call to FromStream, thinking maybe some how the stream was invalid, but that did not trigger an ArgumentInvalid exception, so the stream itself is valid).

1) I tried reducing the method to its simplest form (which takes the incoming stream, makes it an image, and then saves it right back to a new stream), and still get the exception:

public static MemoryStream AddMatting(this MemoryStream stream, Size size, Color color)
{
    MemoryStream newStream = new MemoryStream();
    using (var image = Bitmap.FromStream(stream))
    {
        ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
        using (EncoderParameters encoderParameters = new EncoderParameters(1))
        {
            encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100);
            try
            {
                image.Save(newStream, jpgEncoder, encoderParameters);
            }
            catch (Exception ex)
            {
                if (ex != null) {}
            }
        }
    }
    return newStream;
}

解决方案

The problem is the encoder parameter: Quality must be a long rather than an int[^].


Nevermind. I did it without streams and everything I want to happen is happening.


这篇关于Image.save - 参数无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆