1

I load image using this code:

OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Select a Picture";
ofd.InitialDirectory = @"PATH";
if (ofd.ShowDialog() == DialogResult.OK)
{
      HostImageLocationTxt.Text = ofd.FileName;
      hostImage.Image = new Bitmap(ofd.FileName);
}

then , I load the image in another "PictureBox" and save the image without any modification using this code:

if (transformedImage.Image != null)
            {
                Bitmap bmp = new Bitmap(transformedImage.Image);
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title = "Select Save Location";
                sfd.InitialDirectory = @"PATH";
                sfd.AddExtension = true;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    switch (Path.GetExtension(sfd.FileName).ToUpper())
                    {
                        case ".BMP":
                            bmp.Save(sfd.FileName, ImageFormat.Bmp);
                            break;
                        case ".gif":
                            bmp.Save(sfd.FileName, ImageFormat.Gif);
                            break;
                        case ".JPG":
                            bmp.Save(sfd.FileName, ImageFormat.Jpeg);
                            break;
                        case ".JPEG":
                            bmp.Save(sfd.FileName,ImageFormat.Jpeg);
                            break;
                        case ".PNG":
                            bmp.Save(sfd.FileName, ImageFormat.Png);
                            break;
                        case ".png":
                            bmp.Save(sfd.FileName, ImageFormat.Png);
                            break;
                        default:
                            break;
                    }
                }
            } 

The saved image result in different bit depth (Left: First Load Image , Right: Saved Image): Left: First Load Image , Right: Saved Image

How to save it using same format it was loaded in the first?. Thank you.

Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47
Jake Muller
  • 925
  • 4
  • 18
  • 25
  • You call `ToUpper`, yet you have a case label for both cases...? Also, you leak multiple objects. You should wrap the creation of these objects (OpenFIleDialog, Bitmap, etc.) in a `using` statement. – Cody Gray - on strike Jun 15 '16 at 09:23
  • 2
    8bpp images date from the stone age, back when you had to run Windows in 640 KB of RAM on a 386SUX machine. These days your desktop background image by itself takes more space than the entire Windows 3 install ;) They are quite awkward since they require a palette, System.Drawing supports them very poorly. Most anything you'd do with the image, like creating a copy with the Bitmap constructor, will whack it into modern shape. Don't fix it, it is not 1990 anymore. – Hans Passant Jun 15 '16 at 09:27

1 Answers1

2

You should use this constructor :

Bitmap Constructor (Int32, Int32, PixelFormat)

public Bitmap(
    int width,
    int height,
    PixelFormat format
)

And use this for the pixel format parameter :System.Drawing.Imaging.PixelFormat.Format24bppRgb

EDIT

You can use this to convert your pic (https://stackoverflow.com/a/2016509/5703316):

Bitmap orig = new Bitmap(@"path");
Bitmap clone = new Bitmap(orig.Width, orig.Height,
    System.Drawing.Imaging.PixelFormat.Format24bppRgb);

using (Graphics gr = Graphics.FromImage(clone)) {
    gr.DrawImage(orig, new Rectangle(0, 0, clone.Width, clone.Height));
}
Community
  • 1
  • 1
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
  • You should convert your picture before the save – Quentin Roger Jun 15 '16 at 09:23
  • For 8bpp you'll have to manually copy bytes into the backing array, using LockBits. And you'll need to restore the original image's colour palette, since the actual image contains only indices, not colours. – Nyerguds Jan 05 '18 at 09:18