Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# convert image data to bytes array

I have a image with src = "data:image/png;base64....".

I want to convert this image into byte array byte[].

I have tried something like this:

string[] Base64 = ImageData.Split(new char[] { ',' });

byte[] imageBytes = System.Convert.FromBase64String(Base64[1].ToString());

What am I doing wrong here?

like image 584
pratik Avatar asked Apr 28 '15 14:04

pratik


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

Why is C named so?

After language 'B', Dennis Ritchie came up with another language which was based upon 'B'. As in alphabets B is followed by C and hence he called this language as 'C'.

What is C full form?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

C# 8 version, without using Regex or MemoryStream:

var base64Image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAJFBMVEUAAACAgIAAAAD/AACAAAAAAP8AgAAA/wAAAICAgAD//wD///9wmW9uAAAAAXRSTlMAQObYZgAAAAFiS0dECx/XxMAAAAAHdElNRQfiBhoANycSsUctAAAA60lEQVQoz53Qv07DMBAG8DtH2X2JsscfaaFMIe4TFB4gRLLEzNAHYCA7ElLnbGVkw0+JnYSqDVu/LT9d7o+JrgovQf2rWJbUaiGalZx/J1QzJESfhuhaEGLmujKh/GEUyNwi4dzi0e6A+FdKJVvOK2t3e5ipRSm2qewW/XssaVPKSmlu7HY9geaUM+DJNhMoatlFqXDbv5mxRSsuwwHY969mvLVopbtzL8B6HMuKi2esXLc5TnsQgwt8uu7eD2baXZAF+Pjy3zNQPOQA74e/e0nC2I33OAFxoOOPOX+msNtwAVE0XcoSSOi6/ALgpCd992FfggAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxOC0wNi0yNlQwMDo1NTozOS0wNDowMAwZ2oMAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTgtMDYtMjZUMDA6NTU6MzktMDQ6MDB9RGI/AAAAAElFTkSuQmCC";

var offset = base64Image.IndexOf(',') + 1;

var imageInBytes = Convert.FromBase64String(base64Image[offset..^0]);
like image 190
DK Dhilip Avatar answered Sep 21 '22 20:09

DK Dhilip