c# - FileStream to Byte[]: Windows XP vs Windows 8 -
i had write code that:
- ui takes file (image)
- converts `byte[]
- uploads
byte[]website (usinghttpwebrequest.contenttypemultipart/form-data) - website reads stream, use parameters sent file, stores
byte[]in database.
this image used in reports , user can download image whenever. , worked fine, until started testing on windows xp machine. whenever upload image xp pc, image wouldn't show. after debugging , testing , writing multipart/form-data text file, saw byte[] of file differed on windows xp vs windows 8 (or 7). generated file's size differed well.
i'm using vs2012 .net 4.0, , did install (and repaired again) .net 4 on xp pc. can think either 2 operating systems encode differently or perhaps difference between 32-bit os , 64-bit. have no idea wrong, , don't know start. i'd know if can point me in right direction?
here ui-side code:
//wrapped around each parameter. last boundary after byte[] file >has been omitted. string boundary = "----------------------------" + >datetime.now.ticks.tostring("x"); //creating httpwebrequest multipart "post" method. httpwebrequest = (httpwebrequest)webrequest.create(_webuploadurl); httpwebrequest.contenttype = "multipart/form-data; boundary=" + boundary; httpwebrequest.method = "post"; httpwebrequest.keepalive = true; httpwebrequest.credentials = >system.net.credentialcache.defaultcredentials; //upload stream built parameters, followed >byte[] file. stream uploadstream = new system.io.memorystream(); byte[] boundarybytes = system.text.encoding.ascii.getbytes(boundary + >"\r\n"); string formdatatemplate = "\r\n--" + boundary + "\r\ncontent-disposition: form-data; name=\"{0}\";\r\n\r\n{1}"; //parameters: //foreach parameter, wrap in boundary foreach (string key in _nvcparameters.keys) { string formitem = string.format(formdatatemplate, key, >_nvcparameters[key]); byte[] formitembytes = system.text.encoding.utf8.getbytes(formitem); uploadstream.write(formitembytes, 0, formitembytes.length); } byte[] netbytes = system.text.encoding.ascii.getbytes("\r\n--"); uploadstream.write(netbytes, 0, netbytes.length); //the actual file: uploadstream.write(boundarybytes, 0, boundarybytes.length); string headertemplate = "content-disposition: form-data; name=\"{0}\"; >filename=\"{1}\"\r\n content-type: application/octet-stream\r\n\r\n"; string header = string.format(headertemplate, "uplthefile", _filename); byte[] headerbytes = system.text.encoding.utf8.getbytes(header); uploadstream.write(headerbytes, 0, headerbytes.length); //while file greater buffer, write uploadstream. byte[] buffer = new byte[1024]; int bytesread = 0; long filesize = pfilestream.length; long uploadedvalue = 0; while ((bytesread = (pfilestream).read(buffer, 0, buffer.length)) != 0) { uploadstream.write(buffer, 0, bytesread); application.doevents(); } httpwebrequest.contentlength = uploadstream.length; //close original filestream. pfilestream.close(); the website uses utf8. can post if needed, because byte[]s differ, thought problem might there.
@ramhound use of long? referring filesize , uploadedvalue variables? code part of company's ui-side of system, big , i'm starting know way around parts, anyways, i've checked project's properties, (if right place check). @ build platform target x86. talking or else? sorry i'm still student , new c# (and vs matter)
i've opened both files on windows 8 machine. win8 file size 6kb bigger , contains more characters, obviously.
here first few lines of array. _ncvparameters same (as file uploaded, here first part of resulting multipart-form (ncvparameters followed first few lines of byte[] file):
win8:
------------------------------8d00a632401f30e content-disposition: form-data; name="o";
25 ------------------------------8d00a632401f30e content-disposition: form-data; name="k";
2913 ------------------------------8d00a632401f30e content-disposition: form-data; name="u";
255 ------------------------------8d00a632401f30e content-disposition: form-data; name="f";
blue hills.jpg ------------------------------8d00a632401f30e content-disposition: form-data; name="m";
image/jpeg ------------------------------8d00a632401f30e content-disposition: form-data; name="uplthefile"; filename="blue hills.jpg" content-type: application/octet-stream
‰png
ihdr € à 5ÑÜä srgb ®Îé gama ±üa phys à ÃÇo¨d ÿ¥idatx^ìýg—\7²ëÿ÷¾gïî–§÷¤h9Š¢dr”—Ú{Ç6²”÷m‘yç¹v$™uÅ–úô>÷|˜@˜ÌÄ2y×?oݾøáíÛwþ±õŸ†[w€‘õøû-pëÊßnïãº·ÃØ‡uxk¾í
winxp:
------------------------------8d00a639ff7bf76 content-disposition: form-data; name="o";
25 ------------------------------8d00a639ff7bf76 content-disposition: form-data; name="k";
2913 ------------------------------8d00a639ff7bf76 content-disposition: form-data; name="u";
255 ------------------------------8d00a639ff7bf76 content-disposition: form-data; name="f";
blue hills.jpg ------------------------------8d00a639ff7bf76 content-disposition: form-data; name="m";
image/jpeg ------------------------------8d00a639ff7bf76 content-disposition: form-data; name="uplthefile"; filename="blue hills.jpg" content-type: application/octet-stream
‰png
ihdr ĉ srgb ®Îé gama ±üa chrm z& €„ ú €è u0 ê` :˜ pœºq< idatwc` ªÕÈqiend®b`‚’èÊû:uÊžòÞ°Ë[=)qä¡w¢%º2§Î~󙉬½×f±¤~¯×1‰h$01#ùßÿâ‹ÿ¯¿¸äÿý‡‹—ü;üx§¿8(ýq$?º$ÓÿþéÚêÀbtÿpà%•~ÖbºËá þÝü8ùŸ:å_ø(iÿgã‹þâ/Æ cô¨Í.*›qv
@xaqron - uploadstream memorystream. entire multipart-form written stream, writes byte[]. gets streamed httpwebrequest.
i'm pretty sure same image. i've tried twice make sure. i'll triple-check today @ office - yes, i've done multiple times now. same image produces different sizes , different byte[] characters.
ok, think i've found problem. may have been useful information, didn't think issue. file uploaded image, resize. resizing method takes byte[] (the 1 thought incorrectly encoded), writes bitmap. bitmap 'redrawn' new size pixelformat.format32bpprgb , interpolationmode.highqualitybicubic:
if (pimage.height < maxheight && pimage.width < maxwidth) return pimage; using (pimage) { double xratio = (double)pimage.width / maxwidth; double yratio = (double)pimage.height / maxheight; double ratio = math.max(xratio, yratio); int nnx = (int)math.floor(pimage.width / ratio); int nny = (int)math.floor(pimage.height / ratio); system.drawing.bitmap cpy = new system.drawing.bitmap(nnx, nny, system.drawing.imaging.pixelformat.format32bpprgb); using (system.drawing.graphics gr = system.drawing.graphics.fromimage(cpy)) { gr.clear(system.drawing.color.transparent); gr.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic; gr.drawimage(pimage, new system.drawing.rectangle(0, 0, nnx, nny), new system.drawing.rectangle(0, 0, pimage.width, pimage.height), system.drawing.graphicsunit.pixel); } return cpy; } i changed pixelformat mode 'dontcare' , interpolationmode default. sorry would've been obvious, thought handled .net framework?
windows xp , newer os'es 7 , 8 encoding , decoding things differently. can suggest encode byte array base64 before transferring. receive , decode byte array. .net has easy methods encode , decode base64. since base64 text 7 bit ascii there no problem transferring it.
Comments
Post a Comment