C
C#2y ago
Shane

❔ Help compressing image!

I need help compressing a jpeg that comes in as a base64string. It currently gets compressed via gzip but this is wrong! I want to shrink the image down therefore lowering the res:
using System.IO.Compression;

public static class ImageCompressor
{
// This function takes a base64 string and converts it into a bytes array.
// The bytes array is then saved in memory, is passed into GZIP which compresses it.
// Flush and Close Gzip.
// Then it returns what we saved in memory as an array.
// We then Convert it back into base64 string

public static string Compress(string oldImage)
{
byte[] imageBytes = Convert.FromBase64String(oldImage);
byte[] compressedData = CompressBytes(imageBytes);
string compressedBase64String = Convert.ToBase64String(compressedData);

// For testing
Console.WriteLine(compressedBase64String);
return oldImage;
}

private static byte[] CompressBytes(byte[] inputBytes)
{
using (var ms = new MemoryStream())
{
using (var gzip = new GZipStream(ms, CompressionMode.Compress))
{
gzip.Write(inputBytes, 0, inputBytes.Length);
}

byte[] compressedStream = ms.ToArray();
return compressedStream;
}
}
}
using System.IO.Compression;

public static class ImageCompressor
{
// This function takes a base64 string and converts it into a bytes array.
// The bytes array is then saved in memory, is passed into GZIP which compresses it.
// Flush and Close Gzip.
// Then it returns what we saved in memory as an array.
// We then Convert it back into base64 string

public static string Compress(string oldImage)
{
byte[] imageBytes = Convert.FromBase64String(oldImage);
byte[] compressedData = CompressBytes(imageBytes);
string compressedBase64String = Convert.ToBase64String(compressedData);

// For testing
Console.WriteLine(compressedBase64String);
return oldImage;
}

private static byte[] CompressBytes(byte[] inputBytes)
{
using (var ms = new MemoryStream())
{
using (var gzip = new GZipStream(ms, CompressionMode.Compress))
{
gzip.Write(inputBytes, 0, inputBytes.Length);
}

byte[] compressedStream = ms.ToArray();
return compressedStream;
}
}
}
18 Replies
Angius
Angius2y ago
You'll need something like ImageSharp to resize the image Compression is not resizing
Shane
ShaneOP2y ago
Thanks for responding! How would I implement it? I currently have:

using System.IO.Compression;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats.Jpeg;

public static class ImageCompressor
{
// This function takes a base64 string and converts it into a bytes array.
// The bytes array is then saved in memory, is passed into GZIP which compresses it.
// Flush and Close Gzip.
// Then it returns what we saved in memory as an array.
// We then Convert it back into base64 string

public static string Compress(string oldImage)
{
byte[] imageBytes = Convert.FromBase64String(oldImage);
byte[] compressedData = CompressBytes(imageBytes);
string compressedBase64String = Convert.ToBase64String(compressedData);

// For testing
Console.WriteLine(compressedBase64String);
return oldImage;
}

private static byte[] CompressBytes(byte[] inputBytes)
{
using (var ms = new MemoryStream())
{
// using (var gzip = new GZipStream(ms, CompressionMode.Compress))
// {
// gzip.Write(inputBytes, 0, inputBytes.Length);
// }

using (Image image = Image.Load(ms))
{
int width = image.Width / 2;
int height = image.Height / 2;
image.Mutate(x => x.Resize(width, height));
image.Save(outStream, new JpegEncoder());
}

byte[] compressedStream = ms.ToArray();
return compressedStream;
}
}
}

using System.IO.Compression;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats.Jpeg;

public static class ImageCompressor
{
// This function takes a base64 string and converts it into a bytes array.
// The bytes array is then saved in memory, is passed into GZIP which compresses it.
// Flush and Close Gzip.
// Then it returns what we saved in memory as an array.
// We then Convert it back into base64 string

public static string Compress(string oldImage)
{
byte[] imageBytes = Convert.FromBase64String(oldImage);
byte[] compressedData = CompressBytes(imageBytes);
string compressedBase64String = Convert.ToBase64String(compressedData);

// For testing
Console.WriteLine(compressedBase64String);
return oldImage;
}

private static byte[] CompressBytes(byte[] inputBytes)
{
using (var ms = new MemoryStream())
{
// using (var gzip = new GZipStream(ms, CompressionMode.Compress))
// {
// gzip.Write(inputBytes, 0, inputBytes.Length);
// }

using (Image image = Image.Load(ms))
{
int width = image.Width / 2;
int height = image.Height / 2;
image.Mutate(x => x.Resize(width, height));
image.Save(outStream, new JpegEncoder());
}

byte[] compressedStream = ms.ToArray();
return compressedStream;
}
}
}
but outstream is giving me an error
Angius
Angius2y ago
What error?
Shane
ShaneOP2y ago
Angius
Angius2y ago
Doesn't seem like you have defined outstream anywhere Can't use something that doesn't exist
Shane
ShaneOP2y ago
oh! What is outstream suppose to be?I found the example here: https://docs.sixlabors.com/articles/imagesharp/resize.html
Angius
Angius2y ago
Hover over Save with your mouse and see what the docs say Probably a MemoryStream or FileStream
Shane
ShaneOP2y ago
ah
Shane
ShaneOP2y ago
well I am already using the memory stream so should it be:
Angius
Angius2y ago
Sure, give it a shot
Shane
ShaneOP2y ago
ah failed haha Image cannot be loaded ah ok. Ive changed the Image.Load() to the inputbytes as obviously it needs to receive the input
Shane
ShaneOP2y ago
but the error is now:
Shane
ShaneOP2y ago
@Angius
Angius
Angius2y ago
Seems like the width and/or height divided by 2 give you 0
Shane
ShaneOP2y ago
yes, seems this way should I not divide it by two then? 1.5?
Angius
Angius2y ago
Idk what the original size is
Shane
ShaneOP2y ago
oh crap I think I did it Ill send it one sec
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats.Jpeg;


public static class ImageCompressor
{
// This function takes a base64 string and converts it into a bytes array.
// The bytes array is then saved in memory, is passed into GZIP which compresses it.
// Flush and Close Gzip.
// Then it returns what we saved in memory as an array.
// We then Convert it back into base64 string

public static string Compress(string oldImage)
{
string str = "R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==";
byte[] imageBytes = Convert.FromBase64String(str);
byte[] compressedData = Resize(imageBytes);
string compressedBase64String = Convert.ToBase64String(compressedData);

// For testing
Console.WriteLine(compressedBase64String);
return oldImage;
}

private static byte[] Resize(byte[] inputBytes)
{
using (var inStream = new MemoryStream(inputBytes))
using (var outStream = new MemoryStream())
{
using (Image image = Image.Load(inStream))
{
// double width = image.Width / 1.5;
// double height = image.Height / 1.5;
image.Mutate(x => x.Resize(30, 30));
image.Save(outStream, new JpegEncoder());
}
return outStream.ToArray();
}
}
}
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats.Jpeg;


public static class ImageCompressor
{
// This function takes a base64 string and converts it into a bytes array.
// The bytes array is then saved in memory, is passed into GZIP which compresses it.
// Flush and Close Gzip.
// Then it returns what we saved in memory as an array.
// We then Convert it back into base64 string

public static string Compress(string oldImage)
{
string str = "R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==";
byte[] imageBytes = Convert.FromBase64String(str);
byte[] compressedData = Resize(imageBytes);
string compressedBase64String = Convert.ToBase64String(compressedData);

// For testing
Console.WriteLine(compressedBase64String);
return oldImage;
}

private static byte[] Resize(byte[] inputBytes)
{
using (var inStream = new MemoryStream(inputBytes))
using (var outStream = new MemoryStream())
{
using (Image image = Image.Load(inStream))
{
// double width = image.Width / 1.5;
// double height = image.Height / 1.5;
image.Mutate(x => x.Resize(30, 30));
image.Save(outStream, new JpegEncoder());
}
return outStream.ToArray();
}
}
}
I cna pass in the width and height directly
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server