Is it possible to set the compression level for a jpeg image in WebImage
class? Or would i have to use the Image
class for this?
You have to use the Image to compact it first. After compacted, you could use this on WebImage.
Here is a sample where I convert a base64 to image, but try to understand the compress logic (the 80L is the level of compression):
public MemoryStream Base64ToJpeg(string imgFoto)
{
Image imagemASerAjustada = null;
Bitmap bmpTemporario = null;
MemoryStream imagemCompactadaStream = new MemoryStream();
EncoderParameters myEncoderParameters = new EncoderParameters(1);
try
{
// removemos o cabeçalho a respeito do tipo da imagem
string base64 = imgFoto.Substring(imgFoto.IndexOf(',') + 1);
base64 = base64.Trim('\0');
// convertemos em um array de bytes
byte[] img = Convert.FromBase64String(base64);
// transformamos a base64 em imagem
imagemASerAjustada = Image.FromStream(new MemoryStream(img));
// convertemos a imagem em Bitmap
bmpTemporario = new Bitmap(imagemASerAjustada, new Size(140, 140));
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
Encoder myEncoder = Encoder.Quality;
using (EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 80L))
{
myEncoderParameters.Param[0] = myEncoderParameter;
bmpTemporario.Save(imagemCompactadaStream, jpgEncoder, myEncoderParameters);
}
return imagemCompactadaStream;
}
catch
{
throw;
}
finally
{
if (imagemCompactadaStream.Length > 0)
{
imagemASerAjustada.Dispose();
bmpTemporario.Dispose();
myEncoderParameters.Dispose();
}
}
}
private ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
You can save the Stream in the WebImage(Stream)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.