c# - Check image size using byte array -


i trying upload image server. however, before perform upload perform check see if image size greater 2mb. after receiving image activity result run following code

bitmap bitmap = mediastore.images.media.getbitmap(this.activity.contentresolver, uri);                                 //int imagesize = 79030;                                 //float size = (bitmap.rowbytes * bitmap.height) / 1024;                                 //float seasondrop = size / 10000;                                 //double actualsizemb = math.round(seasondrop, 1);                                 // rotate image if required. samsung devices have exif property rotates image.       bitmap = rotateimageifrequired(bitmap, this.activity, uri);       int size = bitmap.bytecount;       var bytesize = bitmap.rowbytes * bitmap.height;        var buffer = java.nio.bytebuffer.allocate(bytesize);       bitmap.copypixelstobuffer(buffer);       buffer.rewind();       var bytes = new byte[bytesize];       float length = bytes.length / (1024 * 1024); 

i try , upload 1.96mb image. when place break point on length value 10mb. should not possible image increase in size this.

1.9 mb might compressed size image in jpeg or png format (which compressed) if camera or gallery. when check bitmap size, checking uncompressed size of image, larger compressed size. when file reference, check size of file without converting image bitmap. use .net fileinfo.length property check compressed size of image file.

https://msdn.microsoft.com/en-us/library/system.io.fileinfo.length(v=vs.110).aspx

from link above:

// make reference directory.  directoryinfo di = new directoryinfo("c:\\"); // reference each file in directory.  fileinfo[] fiarr = di.getfiles(); // display names , sizes of files.  console.writeline("the directory {0} contains following files:", di.name);  foreach (fileinfo f in fiarr)       console.writeline("the size of {0} {1} bytes.", f.name, f.length); 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -