参数无效错误

本文关键字:错误 无效 参数 | 更新日期: 2023-09-27 17:50:56

我得到的参数无效错误。如何解决这个问题?

以下代码是服务器端
MemoryStream ms = new MemoryStream(ObjDt.ImgUpload);                  
                ms.Write(ObjDt.ImgUpload, 0, ObjDt.ImgUpload.Length);                
                System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);               
                image.Dispose();                
                image.Save(@"D:'Projects'WCF'WCF_ImageUpload'DamagedImages'" + strRandNo + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);                
                // image.Save(@"C:'DotNet'ImageUpload'DamagedImages'" + strRandNo + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                //return strRandNo.ToString();
                string val = strRandNo.ToString();
IPAddress = "localhost:53865/WCF/Image" + val + ".jpg";

在这些代码中,我得到的参数是无效的错误,这些代码的Web配置

<bindings>
      <basicHttpBinding>
        <binding name="StreamedBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Streamed">
          <readerQuotas maxDepth="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
        </binding>
          </basicHttpBinding>
        </bindings>
<services>      
      <service name="Service" behaviorConfiguration="ServiceBehavior">
        <!-- <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicBinding1" contract="WCF_ImageUpload.IService1"></endpoint>-->
        <endpoint address="" binding="basicHttpBinding"
                          bindingConfiguration="StreamedBinding" bindingName="ServiceBehavior"
                          contract="WCF_ImageUpload.IService1" />
      </service>
    </services>

我在Windows应用程序中使用了以下客户端应用程序。

Uri uri = new Uri("http://localhost:53865/Service1.svc/UploadDamagedImage");
            Details ObjDt = new Details();
            ObjDt.Name = "hi";
            ObjDt.Email = "hi@gmail.com";
            ObjDt.ContactNumber = "3698754215";
            ObjDt.DeviceModel = "E23";
            ObjDt.Problem = "Repair";
            ObjDt.Besttimetocontact = "9am";
byte[] bytes = File.ReadAllBytes("D:/WCFImages/admin.png");
ObjDt.ImgUpload = bytes;
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Details));
            // xmlserializer xser = new xmlserializer();
            MemoryStream mem = new MemoryStream();
            ser.WriteObject(mem, ObjDt);
            string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
            WebClient webClient = new WebClient();
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadStringAsync(uri, "POST", data);    

参数无效错误

请告诉我们异常发生的行-同时,请提供堆栈跟踪。以下是我从你发布的代码中快速浏览到的内容

这一行是无效的c#语法

IPAddress = "http://localhost:53865/WCF/Image" + val + ".jpg";

而且,字符串不代表ip地址,所以它不能分配给类型为IPAddress的对象。我假设IPAddress不是变量名,因为c#的命名惯例规定变量名应该以小写字母开头。

除此之外,代码还有缺陷。你正在创建图像,然后你处理它,然后你试图保存它。

System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);               
image.Dispose();                
image.Save(@"D:'Projects'WCF'WCF_ImageUpload'DamagedImages'" + strRandNo + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);         

应该是另一种方式:创建,保存,处置:

System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);               
image.Save(@"D:'Projects'WCF'WCF_ImageUpload'DamagedImages'" + strRandNo + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 
image.Dispose();  

或者更好:

using (System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true))              
{
    image.Save(@"D:'Projects'WCF'WCF_ImageUpload'DamagedImages'" + strRandNo + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 
}

编辑
在你的评论中,你说你从流中加载图像的行会抛出错误。这将导致您应该检查以下内容:

执行以下两行是多余的(在您的情况下甚至是错误的):

MemoryStream ms = new MemoryStream(ObjDt.ImgUpload);                  
ms.Write(ObjDt.ImgUpload, 0, ObjDt.ImgUpload.Length);  

第一行创建了基于ObjDt.ImgUpload中给出的字节的内存流(该流在该行之后已经"包含了图像")。然后再次将字节写入流。要么从流的构造函数中删除形参,要么删除将字节写入流的行。

所有这两行导致流不包含有效的图像数据,因为它在一行中包含两次图像字节。此外,我不确定bool参数是否表示流中存在颜色管理信息-出于测试目的将其删除。代码应该是:

using (MemoryStream ms = new MemoryStream(ObjDt.ImgUpload))
using (System.Drawing.Image image = System.Drawing.Image.FromStream(ms))              
{
    image.Save(@"D:'Projects'WCF'WCF_ImageUpload'DamagedImages'" + strRandNo + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 
}