I'm new to C#, and I'm currently working with this piece of code:
//Take a snapshot from left camera and save to current directory as "snapshot.png"
case Key.Z:
int left = camLeft.Device.LensCorrection1;
camLeft.Device.LensCorrection1 = 0;
Thread.Sleep(150);
BitmapSource bmpSource = camLeft.Device.BitmapSource as BitmapSource;
MemoryStream ms = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmpSource));
encoder.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ms);
string filepath = Environment.CurrentDirectory;
string fileName = System.IO.Path.Combine(filepath, @"snapshot.png");
bitmap.Save(fileName, ImageFormat.Png);
bitmap.Dispose();
camLeft.Device.LensCorrection1 = left;
break;
This is code developed for a camera that, on button press, takes a snapshot and stores it as a png file. This alone works--but what I'm trying to do is have it also take the image data and automatically send it to a PHP webpage that automatically takes in the data and displays the image (bypassing having to store it in a MySQL server). I want this to all work with a single button press--from snapshot being taken, to it all uploading to the webpage to be viewed.
So here's what the above code looks like with the new, problematic/not working code inserted in between the space in the above code:
//Take a snapshot from left camera and save to current directory as "snapshot.png"
case Key.Z:
int left = camLeft.Device.LensCorrection1;
camLeft.Device.LensCorrection1 = 0;
Thread.Sleep(150);
BitmapSource bmpSource = camLeft.Device.BitmapSource as BitmapSource;
MemoryStream ms = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmpSource));
encoder.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
byte[] imageBytes = ms.ToArray();
string base64 = ImageToBase64(imageBytes);
string base64Encoded = HttpUtility.UrlEncode(base64);
WebClient client = new WebClient();
client.UploadString("www.thisismydesiredurl.com", base64Encoded);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ms);
string filepath = Environment.CurrentDirectory;
string fileName = System.IO.Path.Combine(filepath, @"snapshot.png");
bitmap.Save(fileName, ImageFormat.Png);
bitmap.Dispose();
camLeft.Device.LensCorrection1 = left;
break;
The ImageToBase64 is a method that converts the image to base64, as implied:
public string ImageToBase64(byte[] imageBytes)
{
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
The additional code is meant to take the image, convert it into bytes, the convert it into base64, then upload the string via a POST method to a PHP page ready to receive the data. The PHP page then has the following code that's meant to decode and display the image:
<?php
$data = $_POST['base64Encoded'];
$decodedata = urldecode($data);
$rawdata = base64_decode($decodedata);
$source = imagecreatefromstring($rawdata);
?>
<img src= "<?php echo $source ?>" alt="test"/>
But it's not working--no image is displayed, but I know the page is up. What am I missing?
I'm also open to easier/alternate solutions I may not know about. All I want is to have this image automatically viewable on a definite URL with a single button press--that's all.
Edit: The answer provided below "works", but I always get a broken image on my website--so it seems like the image is being uploaded and sent to the website, it's just not being encoded/decoded correctly. Can't seem to figure out why, either. Thoughts?