简体   繁体   English

C#通过Web服务保存图像(字节[])

[英]C# Save image (byte[]) via Webservice

Im trying to make a webservice that can accept a byte[] of an image, and save that to the webserver. 我试图制作一个可以接受图像的byte []的Web服务,并将其保存到Web服务器。 The code consists of two parts, the client that sends the data- and the webservice that gets the data. 该代码包括两部分,即发送数据的客户端和获取数据的Web服务。 Im getting the following error when trying to upload an image: System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: A generic error occurred in GDI+. 我在尝试上传图像时收到以下错误: System.ServiceModel.FaultException`1 [System.ServiceModel.ExceptionDetail]:在GDI +中发生了一般性错误。

myImage is a System.Drawing image. myImage是一个System.Drawing图像。

private byte[] imageToByteArray(Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms,ImageFormat.Png);
        return ms.ToArray();
    }

using (Something.ClientServiceClient client = new Something.ClientServiceClient())
            {
                client.Open();
                client.uploadScreenShot(imageToByteArray(myImage));
            }

The webservice looks as follows: Web服务如下所示:

public void uploadHofScreen( byte[] imgArray)
    {
        Image nImg = byteArrayToImage(imgArray);
        Bitmap bitmap = new Bitmap(nImg);
        saveJpeg("/1/test.jpg", bitmap, 85L);
    }


private void saveJpeg(string path, Bitmap img, long quality)
    {
        EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
        ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");
        if (jpegCodec == null)
            return;
        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;
        img.Save("XXXX" + path, jpegCodec, encoderParams);
    }

    private ImageCodecInfo getEncoderInfo(string mimeType)
    {
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
        for (int i = 0; i < codecs.Length; i++)
            if (codecs[i].MimeType == mimeType)
                return codecs[i];
        return null;
    } 

Both the webconfig and the appconfig has the following readerQuotas: webconfig和appconfig都具有以下阅读器配额:

<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />

Any advice what the issue is? 任何建议是什么问题?

(Just fiddled a little with the permission on the upload folder and noticed that IIS did NOT have write permissions - sorry, I should have checked that before going here) (只是对上传文件夹的权限进行了一些摆弄,并注意到IIS没有写权限-抱歉,在进入这里之前,我应该检查一下该权限)

On the bright side, now you can see how you can upload an image via a webservice. 从好的方面来说,现在您可以看到如何通过Web服务上传图像。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM