Sending image from C# program to PHP webpage for display -
i'm new c#, , i'm working piece of code:
//take snapshot left camera , save current directory "snapshot.png" case key.z: int left = camleft.device.lenscorrection1; camleft.device.lenscorrection1 = 0; thread.sleep(150); bitmapsource bmpsource = camleft.device.bitmapsource 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 code developed camera that, on button press, takes snapshot , stores png file. alone works--but i'm trying have take image data , automatically send php webpage automatically takes in data , displays image (bypassing having store in mysql server). want work single button press--from snapshot being taken, uploading webpage viewed.
so here's above code looks new, problematic/not working code inserted in between space in above code:
//take snapshot left camera , save current directory "snapshot.png" case key.z: int left = camleft.device.lenscorrection1; camleft.device.lenscorrection1 = 0; thread.sleep(150); bitmapsource bmpsource = camleft.device.bitmapsource 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 method converts image base64, implied:
public string imagetobase64(byte[] imagebytes) { // convert byte[] base64 string string base64string = convert.tobase64string(imagebytes); return base64string; }
the additional code meant take image, convert bytes, convert base64, upload string via post method php page ready receive data. php page has following code that's meant decode , display 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 displayed, know page up. missing?
i'm open easier/alternate solutions may not know about. want have image automatically viewable on definite url single button press--that's all.
edit: answer provided below "works", broken image on website--so seems image being uploaded , sent website, it's not being encoded/decoded correctly. can't seem figure out why, either. thoughts?
the way doing it, it's never going work. when make http post, server side scripting meant handle , give output. however, same output not visible when visit script outside of http post request.
what need post data image upload script. script should save file on server , display it.
for example:
c#
using system; using system.collections.generic; using system.linq; using system.text; using system.io; using system.net; using system.drawing; using system.collections.specialized; namespace consoleapplication1 { class program { static void main(string[] args) { // load image system.drawing.image myimage = getimage("http://personal.psu.edu/tao5048/jpg.jpg"); // convert base64 encoded string string base64image = imagetobase64(myimage, system.drawing.imaging.imageformat.jpeg); // post image upload handler using (webclient client = new webclient()) { byte[] response = client.uploadvalues("http://yoursite.com/test.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 byte[] image.save(ms, format); byte[] imagebytes = ms.toarray(); // convert byte[] base64 string string base64string = convert.tobase64string(imagebytes); return base64string; } } } }
php test.php
<?php // handle post if (count($_post)) { // save image file $imagedata = base64_decode($_post['myimagedata']); // write image file $h = fopen('test.jpg', 'w'); fwrite($h, $imagedata); fclose($h); // success exit('image uploaded.'); } // display image if (file_exists('test.jpg')) { echo '<img src="test.jpg" />'; } else { echo "image not uploaded yet."; } ?>
output of c# app is:
after image uploaded, if visit http://yoursite.com/test.php (for example) in browser, see (i.e. uploaded image c#, saved on server, being served back):
hope helps.
Comments
Post a Comment