服务器损坏 图像上传与视窗手机



在使用PhotoChooserTask选择后,我试图将图像上传到我的php服务器,并在服务器中写入文件。我使用UTF8进行编码,但是在服务器中,无论原始图像大小如何,我都只能获得1KB的文件。然后我把它编码成base64string并在服务器中解码。现在我得到了大小为 4/3*图像大小的文件(无需解码),但解码后我无法获得任何图像(文件大小等于原始文件大小)。我尝试了很多方法(用于读取图像),但未能解决这个问题。可能是什么问题?或者我可以建议一些其他方法吗?

客户端中的代码:

  PhotoChooserTask selectphoto = new PhotoChooserTask();
         selectphoto.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
        selectphoto.Show();


     void photoChooserTask_Completed(object sender, PhotoResult e)
                {
                    if (e.TaskResult == TaskResult.OK)
                    {
        System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
                        bmp.SetSource(e.ChosenPhoto);
                      byte[] data = null;
                    using (MemoryStream stream = new MemoryStream())
                        {
                            WriteableBitmap wBitmap = new WriteableBitmap(bmp);
                            wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
                            stream.Seek(0, SeekOrigin.Begin);
                            data = stream.GetBuffer();
                        }
         string utfData = System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.1.49/xampp/imageserver.php");
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    postData = String.Format("image={0}", utfData);   
                    // Getting the request stream.
                    request.BeginGetRequestStream
                        (result =>
                        {
                            // Sending the request.
                            using (var requestStream = request.EndGetRequestStream(result))
                            {
                                using (StreamWriter writer = new StreamWriter(requestStream))
                                {
                                  writer.Write(postData);
                                    writer.Flush();
                                }
                            }
                            // Getting the response.
                            request.BeginGetResponse(responseResult =>
                            {
                                var webResponse = request.EndGetResponse(responseResult);
                                using (var responseStream = webResponse.GetResponseStream())
                                {
                                    using (var streamReader = new StreamReader(responseStream))
                                    {
                                        string srresult = streamReader.ReadToEnd();
                                        System.Diagnostics.Debug.WriteLine("sssssrreeeeeessssulllltttttt========="+srresult);
                                    }
                                }
                            }, null);
                        }, null);
    }

在服务器中:

<?php
if (isset($_POST['image'])) {
 $ifp = fopen( "withoutdecode.txt", "wb" );
    fwrite( $ifp, (($_POST['image'])) );
    fclose( $ifp );
$ifp2 = fopen( "theImage.png", "wb" );
    fwrite( $ifp2, utf8_decode(($_POST['image'])) );
    fclose( $ifp2 );
}
else
{
    die("no image data found");
echo "fail";
}
?>

试试这个:

在WP端(使用图像调整大小算法)

将图像转换为字符串

公共字符串 ImageToByte(BitmapImage imageSource) { MemoryStream ms = new MemoryStream(); 可写位图wb = 新的可写位图(图像源); 工 务 局。SaveJpeg(ms, imageSource.PixelWidth, imageSource.PixelHeight, 0, 100); 字节[] 图像字节 = 毫秒。ToArray();

        string result = Convert.ToBase64String(imageBytes);
        return result;
    }

照片选择器任务已完成

位图图像图像 = 新的位图图像(); 图像。设置源(e.ChosenPhoto);

                string fileName = DateTime.Now.Ticks.ToString() + ".jpg";
                State["filename"] = fileName;
                // Load the picture back into our image
                BitmapImage b = new BitmapImage();
                b.CreateOptions = BitmapCreateOptions.None;
                b.SetSource(e.ChosenPhoto);
                double actualHeight = b.PixelHeight;
                double actualWidth = b.PixelWidth;
                double maxHeight = 600;
                double maxWidth = 800;
                double imgRatio = actualWidth / actualHeight;
                double maxRatio = maxWidth / maxHeight;
                double compressionQuality = 0.5;
                if (actualHeight > maxHeight || actualWidth > maxWidth)
                {
                    if (imgRatio < maxRatio)
                    {
                        //adjust width according to maxHeight
                        imgRatio = maxHeight / actualHeight;
                        actualWidth = imgRatio * actualWidth;
                        actualHeight = maxHeight;
                    }
                    else if (imgRatio > maxRatio)
                    {
                        //adjust height according to maxWidth
                        imgRatio = maxWidth / actualWidth;
                        actualHeight = imgRatio * actualHeight;
                        actualWidth = maxWidth;
                    }
                    else
                    {
                        actualHeight = maxHeight;
                        actualWidth = maxWidth;
                    }
                }
                int newh = Convert.ToInt32(ActualHeight);
                int neww = Convert.ToInt32(actualWidth);
                WriteableBitmap wb = new WriteableBitmap(b);
                WriteableBitmap wb1 = wb.Resize(neww, newh, WriteableBitmapExtensions.Interpolation.Bilinear);
                // Save the image into isolated storage
                using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
                    {
                        WriteableBitmap bitmap = new WriteableBitmap(wb1);
                        bitmap.SaveJpeg(targetStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 70);
                        byte[] content = new byte[e.ChosenPhoto.Length];
                        e.ChosenPhoto.Read(content, 0, content.Length);
                        targetStream.Write(content, 0, content.Length);
                        targetStream.Flush();
                    }
                }
                BitmapImage fbmp = new BitmapImage();
                using (MemoryStream ms = new MemoryStream())
                {
                    wb1.SaveJpeg(ms, neww, newh, 0, 100);
                    fbmp.SetSource(ms);
                }
                string img64 = ImageToByte(fbmp);

将 img64 发送到服务器,就像你在哪里做!它应该可以工作

我终于得到了我的解决方案。

问题是 - 当我使用 Base64 进行编码和解码时,还对网络的一些不安全字符进行了编码。它们是"+"、"/"和"="。在服务器中找不到此字符。因此,我将"+","/"替换为"-","_"并删除了"="。在服务器中,相反的工作在完成(将编辑后的 base64 字符串减回原始 base64 字符串)。现在我在解码后在服务器中获得了图像.

最新更新