Search code examples
c#save-image

Saving an image without ShowFileDialog


My program crop the image that you load from the disk and save as a two jpg. files. I used 2 ShowFileDialog one after another but my professor said it should work differently. First ShowFileDialog it's okay, because we type a file name, and show the path. Second image should have saved without SFD, but with the same path and file name as previous image but with prefix, eg. photo.jpg, photo1.jpg. And here i ask you for help. It should look like [1]: http://scr.hu/5duf/04oep

Here's my code:

        Boolean loaded = true;

        if (loaded)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "jpg(*.jpg)|*.jpg|bmp (*.bmp)|*.bmp| png(*.png) |*.png| gif(*.gif)|*.gif";
            string zapis = sfd.FileName;
            if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName.Length > 0)
            {
                grafika = CropImage (new Bitmap(pictureBox1.Image, pictureBox1.Size), Obcinacz);
                Bitmap Crop1 = new Bitmap(grafika, new Size(SzerReal, WysReal));

                Crop1.Save(sfd.FileName, ImageFormat.Jpeg);
                zapisano = true;
            }

            /////////////////////////////////////////////////////////////////////////////////////////SECOND IMAGE



                try {
                    SzerAnalog = (int)Math.Round((float)SzerMM*300.0f/25.4f, 0); math.round
                    WysAnalog = (int)Math.Round((float)WysMM * 300.0f / 25.4f, 0);
                     }
                catch (Exception) { MessageBox.Show("Błędnie wpisane wartości. Spróbuj użyć innych wymiarów.", "Błąd03", MessageBoxButtons.OK, MessageBoxIcon.Error); }

                try
                {

                    Bitmap grafika2 = new Bitmap(grafika, new Size(SzerAnalog, WysAnalog));
                    grafika2.SetResolution(300.0f, 300.0f);
                    Ark = grafika2;

                }
                catch (Exception) { MessageBox.Show("Zapis zdjęcia analogowego nie powiódł się. Spróbuj zmienić nazwę.", "Błąd04", MessageBoxButtons.OK, MessageBoxIcon.Error); }


            if (DialogResult.Yes == MessageBox.Show("Zdjęcie cyfrowe zostało zapisane. Czy chcesz przygotować arkusz do druku?",
                  "Wydruk", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
            {
               // btnOK = true; 
            }
            else return;
        }



        Arkusz.BackgroundImage = Ark;
        Arkusz.BackgroundImageLayout = ImageLayout.Tile;
    try
            {
                using (Bitmap compositionBmp = new Bitmap(Arkusz.Width, Arkusz.Height))
                {
                    Arkusz.DrawToBitmap(compositionBmp, Unvisible);
                    SaveFileDialog sfd3 = new SaveFileDialog();
                    sfd3.Filter = "jpg (.jpg)|*.jpg|bmp (.bmp)|*.bmp| png(*.png)|*.png| gif (*.gif)|*.gif";
                    compositionBmp.SetResolution(300.0f, 300.0f);
                    if (sfd3.ShowDialog() == DialogResult.OK && sfd3.FileName.Length > 0)
                    {

                        compositionBmp.Save(sfd3.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);


                            MessageBox.Show("Arkusz do wydruku został zapisany", "Zapis.", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }


                }
            }
            catch (Exception)
            {
                MessageBox.Show("Nieudany zapis kompozycji do druku.", "Błąd07", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    }

Solution

  • It is simple a matter of decomposing the first file in its parts and then reassemble the parts adding the required number.

    This could be done using the Path class that offers many methods to work with file paths

    if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName.Length > 0)
    {
        string firstFile = sfd.FileName;
        string secondFile = Path.Combine(Path.GetDirectoryName(firstFile), 
               Path.GetFileNameWithoutExtension(firstFile) + "1" + 
               Path.GetExtension(firstFile));
    
        grafika = CropImage (new Bitmap(pictureBox1.Image, pictureBox1.Size), Obcinacz);
        Bitmap Crop1 = new Bitmap(grafika, new Size(SzerReal, WysReal));
    
        // Now save the first file and the second one
        grafike.Save(firstFile, ImageFormat.Jpeg);
        Crop1.Save(secondFile, ImageFormat.Jpeg);
        zapisano = true;
    }