0

Iam drawing an image to a picturebox. I resize the image in the picturebox as per the width and height to fit it correctly in picutrebox. After that i want to save it, while saving it i also want to save the non image drawn part too in the saved file. Please see the screenshot, in the screenshot i have 2 white portions marked 'X'. when i save the image in picturebox i also want to save the blank portion too (place corssed in red) as either transparent .png or solid white .jpg.

Actually i copied some parts of the code from google and modified. It will be great and so much helpful if someone please explain it line by line.

enter image description here


THis is what i have donw so far,

private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog open = new OpenFileDialog();
        open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp;  *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
        if (open.ShowDialog() == DialogResult.OK)
        {               
            Image original = Bitmap.FromFile(open.FileName);
            pictureBox1.Image = new Bitmap(ScaleImage(original));

            pictureBox1.Padding = new Padding((pictureBox1.Width - ScaleImage(original).Width) / 2, (pictureBox1.Height - ScaleImage(original).Height) / 2, 0, 0);
        }
    }

    private Bitmap ScaleImage(Image oldImage)
    {
        double resizeFactor = 1;

        if (oldImage.Width > 300 || oldImage.Height > 300)
        {
            double widthFactor = Convert.ToDouble(oldImage.Width) / 300;
            double heightFactor = Convert.ToDouble(oldImage.Height) / 125;
            resizeFactor = Math.Max(widthFactor, heightFactor);
        }

        int width = Convert.ToInt32(oldImage.Width / resizeFactor);
        int height = Convert.ToInt32(oldImage.Height / resizeFactor);
        Bitmap newImage = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        newImage.MakeTransparent(Color.White);

        Graphics g = Graphics.FromImage(newImage);

        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.DrawImage(oldImage, 0, 0, newImage.Width, newImage.Height);
        return newImage;
    }
    private void button2_Click(object sender, EventArgs e)
    {
        pictureBox1.BackColor = Color.White;
        pictureBox1.Image.Save("D:\\temp.png", ImageFormat.Png);
    }
locknies
  • 1,345
  • 3
  • 15
  • 36
  • Currently iam saving image in picturebox. I think i had to save the whole picturebox or something like this. When i save i also want to keep my image intact. Add transparency or solid color in the crossed place. – locknies Sep 10 '15 at 07:05
  • If some other suggest other options for adding image, resizing and save it without picturebox are also welcome! – locknies Sep 10 '15 at 07:06
  • I can't really see what the goal is here. It is to save the resized picture, or to display the picture in the picture box? So what you basically want is to resize the picture to fit some dimensions with heaping the ratio and drag white or transparent in the blanks? – Matyas Sep 10 '15 at 07:20
  • The goal is to save the resized picture in picturebox, by keeping image size intact and saved picture's height and width same as picturebox with the crossed space either transparent (png) or any solid color (jpg). – locknies Sep 10 '15 at 07:22

1 Answers1

3

I hope that this might help. Try to use it to save the resized image (in button2_Click I guess).

using (Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height))
{
    using (Graphics graphics = Graphics.FromImage(bitmap))
    {
        graphics.Clear(Color.Transparent);
        graphics.DrawImage(pictureBox1.Image, (bitmap.Width - pictureBox1.Image.Width) / 2, (bitmap.Height - pictureBox1.Image.Height) / 2);
    }

    bitmap.Save(@"D:\tmpMod.png", ImageFormat.Png);
}
Matyas
  • 1,122
  • 5
  • 23
  • 29