-1

I am working on a project in C# and I need to know how to validate an image in C# to be of a specific size?

For example the image to be uploaded must be less than 25Kb.

Enea Dume
  • 3,014
  • 3
  • 21
  • 36
  • How do taking an image from UI is that with `HttpPostedFile` or `Base64ImageString` – Prashant Pimpale Jul 28 '18 at 10:46
  • "The Image to be uploaded" means, you want to check the size before upload? Do you want to do this in an c# application or on a website in JavaScript? Or do you want to check the image size on the server side? – Christoph Lütjen Jul 28 '18 at 10:50
  • i need it for a c# application. once the image is chosen, i should get to know whether the image is of specified size or not. if yes, then it should accept the image and if no, then it should ask the user to try for some other image. Its similar to what we do while filing an online form and upload an image or signature. – Prachi Sharma Jul 28 '18 at 10:55
  • @PrachiSharma could pls be more specific on >C# application. Is that asp.net, windows etc? – Prashant Pimpale Jul 28 '18 at 10:57
  • Jst take a look at :https://stackoverflow.com/a/12570870/7124761 – Prashant Pimpale Jul 28 '18 at 11:02
  • it is windows application @PrashantPimpale – Prachi Sharma Jul 28 '18 at 11:08
  • @Prachi Two way to do that: on the client side (Javascript)-- The link shared by me and on the server side (C#) -- The link shared by OrelEraki – Prashant Pimpale Jul 28 '18 at 11:15

1 Answers1

1

FileInfo.Length will return the current file size in bytes.

long length = new System.IO.FileInfo(path).Length;

25 Kilobytes (even though your question implicitly asks about Kilobits), is 25000 bytes. Check if less than or greater than that value.

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43