我得到参数无效error.how来解决这个问题?
以下代码是服务器端
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);
答案 0 :(得分: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);
}