I’m having a problem with saving a picture. With this code I’m putting two pictures together. But when I save them the quality is worse than the quality of the original pictures.
Image Image1 = Image.FromFile(openFileDialogOpenPicture1.FileName);
Image Image2 = Image.FromFile(openFileDialogOpenPicture2.FileName);
int imageHeight = 0;
if (Image1.Height > Image2.Height)
imageHeight = Image1.Height;
else
imageHeight = Image2.Height;
Bitmap finalImage = new System.Drawing.Bitmap(Image1.Width + Image2.Width, imageHeight);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
{
//set background color
g.Clear(System.Drawing.Color.Black);
//go through images and draw it on the final image
int offset = 0;
g.DrawImage(pictureBoxBackground1.Image, new System.Drawing.Rectangle(offset, 0, Image1.Width, Image1.Height));
offset += Image1.Width;
g.DrawImage(pictureBoxBackground2.Image, new System.Drawing.Rectangle(offset, 0, Image2.Width, Image2.Height));
var eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
var ici = GetEncoderInfo("image/jpeg");
finalImage.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\TestImage.jpg", ici, eps);
}
I’ve also tried to save the picture uncompressed, but that only makes file file bigger and not the quality better.
Thank you in advance!
I have used this GetEncoder method:
Invoked it with GetEncoder(ImageFormat.Jpeg); and removed the references to pictureBoxBackground1&2 (otherwise it won’t build) and the end result is high quality as far as I can tell.