C
C#2y ago
Binto86

✅ Image from dataurl

I have dataurl with image and i need to get it to some reasonable image object in c#, so i can resize it and get bytes from the resized image. I was playing with skiasharp bitmap, but i couldn't figure it out.
20 Replies
Lexaro
Lexaro2y ago
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ImageConversionToHelpBinto86
{
class Program
{
static void Main(string[] args)
{
string dataUrl = "";

string base64 = dataUrl.Split(',')[1];

byte[] imageBytes = Convert.FromBase64String(base64);

using (MemoryStream ms = new MemoryStream(imageBytes))
{
Image image = Image.FromStream(ms);

Bitmap resizedImage = new Bitmap(image, new Size(100, 100));

byte[] resizedImageBytes;
using (MemoryStream resizedMs = new MemoryStream())
{
resizedImage.Save(resizedMs, ImageFormat.Jpeg);
resizedImageBytes = resizedMs.ToArray();
}

}
}
}
}
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ImageConversionToHelpBinto86
{
class Program
{
static void Main(string[] args)
{
string dataUrl = "";

string base64 = dataUrl.Split(',')[1];

byte[] imageBytes = Convert.FromBase64String(base64);

using (MemoryStream ms = new MemoryStream(imageBytes))
{
Image image = Image.FromStream(ms);

Bitmap resizedImage = new Bitmap(image, new Size(100, 100));

byte[] resizedImageBytes;
using (MemoryStream resizedMs = new MemoryStream())
{
resizedImage.Save(resizedMs, ImageFormat.Jpeg);
resizedImageBytes = resizedMs.ToArray();
}

}
}
}
}
@Binto86 use my code to convert a data URL to an image in C#
Binto86
Binto86OP2y ago
thanks, unfortunately im not on windows, this is wasm, so i can't really use System.Drawing
Lexaro
Lexaro2y ago
macOS? or Linux?
Binto86
Binto86OP2y ago
otherwise, im doing the same to extract the bytes from the url so... webassembly
Lexaro
Lexaro2y ago
okay one second @Binto86 so i try to write a code for u with SkiaSharp
Binto86
Binto86OP2y ago
thank you i did try skiasharp but i couldn't get it to work with SKBitmap.Decode or anything im trying ImageSharp now
Lexaro
Lexaro2y ago
using System;
using System.IO;
using SkiaSharp;

namespace ImageConversion
{
public class Program
{
public static byte[] ConvertDataUrlToImageBytes(string dataUrl)
{
string base64 = dataUrl.Split(',')[1];

byte[] imageBytes = Convert.FromBase64String(base64);

using (MemoryStream ms = new MemoryStream(imageBytes))
{
using (SKManagedStream skStream = new SKManagedStream(ms))
using (SKBitmap bitmap = SKBitmap.Decode(skStream))
{
using (SKBitmap resizedBitmap = new SKBitmap(100, 100))
using (SKCanvas canvas = new SKCanvas(resizedBitmap))
{
canvas.DrawBitmap(bitmap, new SKRect(0, 0, 100, 100), BitmapStretch.Uniform);

using (MemoryStream resizedMs = new MemoryStream())
{
SKImage.FromBitmap(resizedBitmap).Save(resizedMs, SKEncodedImageFormat.Jpeg);
return resizedMs.ToArray();
}
}
}
}
}
}
}
using System;
using System.IO;
using SkiaSharp;

namespace ImageConversion
{
public class Program
{
public static byte[] ConvertDataUrlToImageBytes(string dataUrl)
{
string base64 = dataUrl.Split(',')[1];

byte[] imageBytes = Convert.FromBase64String(base64);

using (MemoryStream ms = new MemoryStream(imageBytes))
{
using (SKManagedStream skStream = new SKManagedStream(ms))
using (SKBitmap bitmap = SKBitmap.Decode(skStream))
{
using (SKBitmap resizedBitmap = new SKBitmap(100, 100))
using (SKCanvas canvas = new SKCanvas(resizedBitmap))
{
canvas.DrawBitmap(bitmap, new SKRect(0, 0, 100, 100), BitmapStretch.Uniform);

using (MemoryStream resizedMs = new MemoryStream())
{
SKImage.FromBitmap(resizedBitmap).Save(resizedMs, SKEncodedImageFormat.Jpeg);
return resizedMs.ToArray();
}
}
}
}
}
}
}
@Binto86 try that @Binto86 working?
Binto86
Binto86OP2y ago
my SKImage doesn't have Save function im trying to solve that ok, did this instad
SKData d = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100);
d.SaveTo(resizedMs);
SKData d = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100);
d.SaveTo(resizedMs);
now im getting the bytes back i just can't tell if its actually resized or not anyways, thank you very much
Lexaro
Lexaro2y ago
You can check the size of the resized image by comparing the width and height properties of the original SKBitmap and the resized SKBitmap.
Binto86
Binto86OP2y ago
i don't think it was but i changed the code a bit so hopefully
Lexaro
Lexaro2y ago
okay text me if u need help again
Binto86
Binto86OP2y ago
yes it works tysm
Lexaro
Lexaro2y ago
😊 No Problem
Binto86
Binto86OP2y ago
the skiasharp lib is acting really weird and unexpectedly
Lexaro
Lexaro2y ago
What is happening? How long have you been programming with csharp? @Binto86
Binto86
Binto86OP2y ago
rn nothing as its solved 2 years
Lexaro
Lexaro2y ago
okok
Binto86
Binto86OP2y ago
the api of the lib is just weird imo
Lexaro
Lexaro2y ago
i correct the code wait
using System;
using System.IO;
using SkiaSharp;

namespace ImageConversion
{
public class Program
{
public static byte[] ConvertDataUrlToImageBytes(string dataUrl)
{
string base64 = dataUrl.Split(',')[1];

byte[] imageBytes = Convert.FromBase64String(base64);

using (MemoryStream ms = new MemoryStream(imageBytes))
{
using (SKManagedStream skStream = new SKManagedStream(ms))
using (SKBitmap bitmap = SKBitmap.Decode(skStream))
{
using (SKBitmap resizedBitmap = new SKBitmap(100, 100))
using (SKCanvas canvas = new SKCanvas(resizedBitmap))
{
canvas.DrawBitmap(bitmap, new SKRect(0, 0, 100, 100), BitmapStretch.Uniform);

using (MemoryStream resizedMs = new MemoryStream())
{
SKData d = SKImage.FromBitmap(resizedBitmap).Encode(SKEncodedImageFormat.Png, 100);
d.SaveTo(resizedMs);
return resizedMs.ToArray();
}
}
}
}
}
}
}
using System;
using System.IO;
using SkiaSharp;

namespace ImageConversion
{
public class Program
{
public static byte[] ConvertDataUrlToImageBytes(string dataUrl)
{
string base64 = dataUrl.Split(',')[1];

byte[] imageBytes = Convert.FromBase64String(base64);

using (MemoryStream ms = new MemoryStream(imageBytes))
{
using (SKManagedStream skStream = new SKManagedStream(ms))
using (SKBitmap bitmap = SKBitmap.Decode(skStream))
{
using (SKBitmap resizedBitmap = new SKBitmap(100, 100))
using (SKCanvas canvas = new SKCanvas(resizedBitmap))
{
canvas.DrawBitmap(bitmap, new SKRect(0, 0, 100, 100), BitmapStretch.Uniform);

using (MemoryStream resizedMs = new MemoryStream())
{
SKData d = SKImage.FromBitmap(resizedBitmap).Encode(SKEncodedImageFormat.Png, 100);
d.SaveTo(resizedMs);
return resizedMs.ToArray();
}
}
}
}
}
}
}
this should be a better version of the code and should work fine @Binto86
Binto86
Binto86OP2y ago
yes this is what i changed your original code to and works just fine

Did you find this page helpful?