Convert Raw URI to displayable image C#

Viewed 31

The Issue

I am using A TOTP Library That Returns URI Data For The Scannable QR Code And I Need Help To Get The Code Either To Display In Web Browser Object In Windows Forms Or As An Image.

This Is The Data That I Get: The Gist Containing The Data

The Expected Outcome

Either The QR Code Displayed In A C# .Net Framework Windows Forms WebBrowser Component Or The QR Code As A BMP Or PNG File

What I Have Tried

I Have Tried To Download The QR By Using WebClient And WebClient.openRead(URI);

To Which I Got The Following Error: The Error

I Have Also Tried To Use The WebBrowser Component By Passing In The URI And Then Refreshing, Show.

To Which There Was No Display, Or It Failed To Webpage


I am At A Loss Here As I Have Googled For An Answer But Returned Blank

1 Answers

The Following Is For Converting To Image File Only And Not Displaying In WebBrowser. This Has Been Tested And Is Working Correctly.

Inside The First Gist Link Is A Data Scheme URL/URI All You Need To Do Is The Following.

  1. Convert The URL/URI Into A String.
  2. Split The String With The Char Delimiter Of ','.
  3. Retrieve The String At Index 1 and not 0.
  4. Convert The String Using Convert.fromBase64String Passing In The String Containing The String At Index 1
  5. Create A Temporary With The .png Extension.
  6. Write The Byte Array Into The File.

And That Will Save The Image Into A File For Easy Retrieval Using Image.FromFile.

 Uri Data = new Uri(Setup.QrCodeImage); //This Is Located From A TOPT Generator
 string[] Objects = Data.ToString().Split(',');
 string Base64String = Objects[1];
 byte[] ImageD = Convert.FromBase64String(Base64String);
 string P = Path.GetTempFileName() + ".png";
 File.WriteAllBytes(P, ImageD);
 Image image = Image.FromFile(P);

If You Found This Helpful I'm Glad.

Related