0

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

以下代码是服务器端

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";

在这些代码中,我得到 Parameter is not valid error..这些代码的 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);    
4

1 回答 1

0

请告诉我们发生异常的行 - 另外,请提供堆栈跟踪。以下是我快速浏览您发布的代码所看到的...

此行不是有效的 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); 
}
于 2013-01-08T12:05:22.673 回答