I have a c# application that contains an image gallery where I display some pictures.
This gallery have some features including left and right rotation.
everything is perfect but when I choose a picture from gallery and press rotation button (regardless left or right rotation), size of the picture increase significantly.
It should be mentioned that the picture’s format is JPEG.
Size of picture before rotation : 278 kb
Size of picture after rotation : 780 kb
My code for rotation is like bellow :
public Image apply(Image img)
{
Image im = img;
if (rotate == 1) im.RotateFlip(RotateFlipType.Rotate90FlipNone);
if (rotate == 2) im.RotateFlip(RotateFlipType.Rotate180FlipNone);
if (rotate == 3) im.RotateFlip(RotateFlipType.Rotate270FlipNone);
//file size is increasing after RotateFlip method
if (brigh != DEFAULT_BRIGH ||
contr != DEFAULT_CONTR ||
gamma != DEFAULT_GAMMA)
{
using (Graphics g = Graphics.FromImage(im))
{
float b = _brigh;
float c = _contr;
ImageAttributes derp = new ImageAttributes();
derp.SetColorMatrix(new ColorMatrix(new float[][]{
new float[]{c, 0, 0, 0, 0},
new float[]{0, c, 0, 0, 0},
new float[]{0, 0, c, 0, 0},
new float[]{0, 0, 0, 1, 0},
new float[]{b, b, b, 0, 1}}));
derp.SetGamma(_gamma);
g.DrawImage(img, new Rectangle(Point.Empty, img.Size),
0, 0, img.Width, img.Height, GraphicsUnit.Pixel, derp);
}
}
return im;
}
What is the problem?
In your case applying
RotateFliponimis changing theImageFormatfromJpegtoMemoryBmp.By Default when you save the image it is going to make use of the default
ImageFormat. This will be the format returned byim.RawFormatif you check the GUID
im.RawFormat.GuidBefore RotateFlip
{b96b3cae-0728-11d3-9d7b-0000f81ef32e}
which is same as
ImageFormat.Jpeg.GuidAfter RotateFlip
{b96b3caa-0728-11d3-9d7b-0000f81ef32e}
which is same as
ImageFormat.MemoryBmp.GuidAt the time of saving the image pass the
ImageFormatas the second parameter which will ensure that it uses the correct format. If not mentioned it is going to be the one inim.RawFormatSo If you want to save as jpeg at the time of saving call
This time the file size should be less than the original size.
Also note
ImageFormatis inSystem.Drawing.ImagingnamespaceNOTE
To control the quality of the jpeg make use of the overloaded Save method as mentioned in this MSDN Link
EDIT Based On Comment
OK assuming you are using SQL Server you must be having a
imagedatatype column (it is recommended to usevarbinary(max)instead ofimageas in future it is going to be obselete (Read MSDN Post)Now to the steps