WebClient.UploadFile是一个位图?

时间:2016-03-04 14:27:01

标签: c# php file-upload bitmap webclient

如果这与其他问题相似,或者问题已经得到解答,我真的很抱歉,但我无法做到这一点。

Bitmap image = new Bitmap(iw, ih, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(image);
g.CopyFromScreen(ix+left, iy,0,0, new System.Drawing.Size(iw, ih), CopyPixelOperation.SourceCopy);

MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Png);
byte[] bitmapBytes = memoryStream.GetBuffer();
string bitmapString = Convert.ToBase64String(bitmapBytes, Base64FormattingOptions.InsertLineBreaks);

try
{
    System.Net.WebClient Client = new System.Net.WebClient();
    Client.Headers.Add("Content-Type", "binary/octet-stream");
    byte[] result = Client.UploadFile("http://localhost/image/index.php", "POST", bitmapString);
    string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);
} catch (Exception ex)
{
    System.Diagnostics.Trace.WriteLine(ex + " BITMAPSTR: "+bitmapString);
}

我正在截取屏幕截图然后我想通过php文件将其上传到我的服务器。哪个应该保存文件,如果我在计算机上指定一个已经保存的文件,那就可以了,但是我无法使用位图。

if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["file"]["tmp_name"];
    $name = $_FILES["file"]["name"];
    move_uploaded_file($tmp_name, "./$name");
}

如何转换位图以使其适用于WebClient UploadFile?

修改

抛出错误(已翻译,可能不是确切的单词):

System.Net.WebException: An exception occured during a WebClient-request. ---> System.ArgumentException: Invalid characters in path.

1 个答案:

答案 0 :(得分:1)

试试这个。

<强> C#

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load a bitmap image here
            // your code here

            // Convert to base64 encoded string
            string base64Image = ImageToBase64(myImage, System.Drawing.Imaging.ImageFormat.Jpeg);

            // Post image to upload handler
            using (WebClient client = new WebClient())
            {
                byte[] response = client.UploadValues("http://localhost/image/index.php", new NameValueCollection()
                {
                    { "myImageData", base64Image }
                });

                Console.WriteLine("Server Said: " + System.Text.Encoding.Default.GetString(response));
            }

            Console.ReadKey();
        }

        static System.Drawing.Image GetImage(string filePath)
        {
            WebClient l_WebClient = new WebClient();
            byte[] l_imageBytes = l_WebClient.DownloadData(filePath);
            MemoryStream l_stream = new MemoryStream(l_imageBytes);
            return Image.FromStream(l_stream);
        }

        static string ImageToBase64(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // Convert Image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                // Convert byte[] to Base64 String
                return Convert.ToBase64String(imageBytes);
            }
        }
    }
}

<强> PHP

// Handle Post
if (count($_POST))
{
    // Save image to file
    $imageData = base64_decode($_POST['myImageData']);

    // Write Image to file
    $h = fopen('test.jpg', 'w');
    fwrite($h, $imageData);
    fclose($h);

    // Success
    exit('Image successfully uploaded.');
}

参考:https://stackoverflow.com/a/23937760/2332336(我之前的回答之一)