Shane
Shane
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
I cna pass in the width and height directly
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
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();
}
}
}
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
Ill send it one sec
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
oh crap I think I did it
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
1.5?
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
should I not divide it by two then?
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
yes, seems this way
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
@Angius
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
ah ok. Ive changed the Image.Load() to the inputbytes as obviously it needs to receive the input
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
Image cannot be loaded
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
ah failed haha
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
ah
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
oh! What is outstream suppose to be?I found the example here:
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
but outstream is giving me an error
31 replies
CC#
Created by Shane on 12/17/2022 in #help
❔ Help compressing image!
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;
}
}
}
31 replies