Hello I have this tutorial to try to get an image compressed, however, when I use a profile photo with my phone(selfie) it just rotates 45 degrees.
What could be happening?
EDIT
I added the code to show you, so it is not necessary to click the link
private void VaryQualityLevel()
{
// Get a bitmap. The using statement ensures objects
// are automatically disposed from memory after use.
using (Bitmap bmp1 = new Bitmap(#"C:\TestPhoto.jpg"))
{
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;
// 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.
EncoderParameters myEncoderParameters = new EncoderParameters(1);
// Save the bitmap as a JPG file with 10 quality level compression.
myEncoderParameter = new EncoderParameter(myEncoder, 10L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(#"C:\TestPhotoQualityZero.jpg", jpgEncoder, myEncoderParameters);
}
}
This is the other portion of code:
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
Below is my code to capture a screenshot. I tried to use "EncoderParameters" to reduce the file size but it seems doesn't work.
My screen resolution: 1920 x 1080:
Without Encoder setting, just save the bitmap image directly: 75KB
Encoder JPG 100%: 350KB
Encoder JPG 50%: 92KB
Encoder JPG 0%: 40KB (Can't see anything with this setting)
Is there anyway to reduce the image size less than 10KB? If not, what is the best method to reduce the file size as much as possible?
Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graph = Graphics.FromImage(bmp as Image);
graph.CopyFromScreen(0, 0, 0, 0, bmp.Size);
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp.Save("TestPhotoQualityFifty.jpg", jpgEncoder, myEncoderParameters);
myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp.Save("TestPhotoQualityHundred.jpg", jpgEncoder, myEncoderParameters);
myEncoderParameter = new EncoderParameter(myEncoder, 0L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp.Save("TestPhotoQualityZero.jpg", jpgEncoder, myEncoderParameters);
The problem is I want to send a desktop screenshot(which is Bitmap type) via UDP. As you know UDP has limited capacity to 65 000 bytes, so I have to "compress" image size to fit this capacity. I tried this:
MemoryStream memoryStream = new MemoryStream();
Bitmap bmp = CaptureDesktopWithCursor2();
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 15L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp.Save(memoryStream, jpgEncoder, myEncoderParameters);
Bitmap Bit;
using (memoryStream)
{
Bit = new Bitmap(memoryStream);
byte[] lol = imageToByteArray(Bit);
}
It does change size of image, but only when you save it as .jpg file for example.
Is there anything else what I can do to fit UDP capacity(65 000)? Of course I tried resizing image(it fits cap well then) but it affects quality of this image much.
public byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
I have this code that saves an image to disk
public static void SaveImage(Bitmap picture, string path)
{
// Comprimir
ImageCodecInfo encoder = GetJPGEncoder();
// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;
// 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.
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
//picture.
picture.Save(path, encoder, myEncoderParameters);
}
but now I need to get the result as a byte[]. I can't find a way to get the result in memory so that I can convert it to byte[]. I thought about loading the file after saving it in the harddrive, but that would be completely unnecessary. Does anyone know if I can achieve this?
Use this:
using(var stream = new MemoryStream())
{
picture.Save(stream, encoder, myEncoderParameters);
return stream.ToArray();
}
Use a MemoryStream. One of the overloads of the Save() method takes a Stream.
I need to get a picture from the user, with different format extensions, and I want to always save it as "jpg", for easy handling.
is there a good way do that in c# without arming the quality?
From: http://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoderparameter.aspx
private void VaryQualityLevel()
{
// Get a bitmap.
Bitmap bmp1 = new Bitmap(#"c:\TestPhoto.jpg");
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;
// 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.
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder,
50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(#"c:\TestPhotoQualityFifty.jpg", jpgEncoder,
myEncoderParameters);
myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(#"c:\TestPhotoQualityHundred.jpg", jpgEncoder,
myEncoderParameters);
// Save the bitmap as a JPG file with zero quality level compression.
myEncoderParameter = new EncoderParameter(myEncoder, 0L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(#"c:\TestPhotoQualityZero.jpg", jpgEncoder,
myEncoderParameters);
}
...
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
I use this simple extension to convert a stream, all it does is convert it though and does nothing for quality.
public static Stream ConvertImage(this Stream originalStream, ImageFormat format)
{
var image = Image.FromStream(originalStream);
var stream = new MemoryStream();
image.Save(stream, format);
stream.Position = 0;
return stream;
}
usage:
var outputStream = gifStream.ConvertImage(ImageFormat.Png);
Windows GDI+ provides the Image class and the Bitmap class for storing
images in memory and manipulating images in memory. GDI+ writes images
to disk files with the help of image encoders and loads images from
disk files with the help of image decoders.
Check Image.Save Method overloads to implement your functionality. which support various image format of type ImageFormat Class.
follow example on Image.Save Method (String, ImageCodecInfo, EncoderParameters), which will let you save Jpg file with encoding etc.
code snippet:
using System;
using System.Drawing;
using System.Drawing.Imaging;
class Example_SetJPEGQuality
{
public static void Main()
{
Bitmap myBitmap;
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
// Create a Bitmap object based on a BMP file.
myBitmap = new Bitmap("Shapes.bmp");
// Get an ImageCodecInfo object that represents the JPEG codec.
myImageCodecInfo = GetEncoderInfo("image/jpeg");
// Create an Encoder object based on the GUID
// for the Quality parameter category.
myEncoder = Encoder.Quality;
// 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.
myEncoderParameters = new EncoderParameters(1);
// Save the bitmap as a JPEG file with quality level 25.
myEncoderParameter = new EncoderParameter(myEncoder, 25L);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters);
// Save the bitmap as a JPEG file with quality level 50.
myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save("Shapes050.jpg", myImageCodecInfo, myEncoderParameters);
// Save the bitmap as a JPEG file with quality level 75.
myEncoderParameter = new EncoderParameter(myEncoder, 75L);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save("Shapes075.jpg", myImageCodecInfo, myEncoderParameters);
}
// Codec info
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
}
Hope this help..
Try this:
public static class BitmapExtension
{
public static void Save(this Bitmap bitmap, String fileName, ImageFormat imageFormat, long quality = 75L)
{
using (var encoderParameters = new EncoderParameters(1))
using (encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality))
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
bitmap.Save(fileName, codecs.Single(codec => codec.FormatID == imageFormat.Guid), encoderParameters);
}
}
}
...
Bitmap bitmap = new Bitmap("myImage.bmp");
bitmap.Save("myImage.jpg", ImageFormat.Jpeg, 50L);