How to Save Picturebox.Image to File

How to save PictureBox.Image to file?

Try this

pictureBox.Image.Save(@"Path",ImageFormat.Jpeg);

Saving Image From Picturebox

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);
}

Saving an image from picture box to png C#

You are getting NullReferenceException exception because the Image property of the picturebox is null.

Try PictureBox Load method like:

pictureBox1.ImageLocation = @"C:\emo\hairs\Hair-01.png";
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Load();
pictureBox1.Image.Save(@"C:\Coke\res1.png", System.Drawing.Imaging.ImageFormat.Png);

Another alternative is to use another overload of the Load() method:

pictureBox1.Load(@"C:\emo\hairs\Hair-01.png");
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image.Save(@"C:\Coke\res1.png", System.Drawing.Imaging.ImageFormat.Png);

How to save a picturebox control as a jpeg file after it's edited

You probably shouldn't draw directly on the PictureBox.

You need to use a Bitmap instead. Try putting the bitmap in the PictureBox.Image and then call Save().

Check this for more details

How to save graphics created on a PictureBox?

Never use control.CreateGraphics! Either draw into a Bitmap bmp using a Graphics g = Graphics.FromImage(bmp) or in the Paint event of a control, using the e.Graphics parameter..

Here is a cropping code that draws into a new Bitmap and that makes use of your controls etc but changes a few things:

  • It uses a Graphics object that is created from a new Bitmap
  • It make use of using clauses to make sure it won't leak
  • It takes the size of the pictureBox3.ClientSize so it won't include any borders..

private void crop_bttn_Click(object sender, EventArgs e)
{
Image crop = GetCopyImage("grayScale.jpg");
pictureBox2.Image = crop;
Bitmap targetBitmap = new Bitmap(pictureBox3.ClientSize.Width,
pictureBox3.ClientSize.Height);
using (Bitmap sourceBitmap = new Bitmap(pictureBox2.Image,
pictureBox2.ClientSize.Width, pictureBox2.ClientSize.Height))
{
using (Graphics g = Graphics.FromImage(targetBitmap))
{
g.DrawImage(sourceBitmap, new Rectangle(0, 0,
pictureBox3.ClientSize.Width, pictureBox3.ClientSize.Height),
rectCropArea, GraphicsUnit.Pixel);
}
}
if (pictureBox3.Image != null) pictureBox3.Image.Dispose();
pictureBox3.Image = targetBitmap;
targetBitmap.Save(somename, someFormat);
}

The alternative would be to..:

  • move all your code to the Paint event
  • replace the Graphics g = pictureBox3.CreateGraphics(); be Graphics g = e.Graphics;
  • insert these two lines to the click event:

Bitmap targetBitmap = new Bitmap(pictureBox3.ClientSize.Width, 
pictureBox3.ClientSize.Height);
pictureBox3.DrawToBitmap(targetBitmap, pictureBox3.ClientRectangle);

Saving picturebox WITH backgroundimage to file

You can combine two images using graphics or bitmap. There is a good example provided here

Bitmap

 using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var watermark = new Bitmap(@"Images\watermark.png"))
{
//Make the watermark semitransparent.
watermark.Channels.ScaleAlpha(0.8F);

//Watermark image
bitmap.Draw(watermark, 10, bitmap.Height - watermark.Height - 40, CombineMode.Alpha);

//Save the resulting image
bitmap.Save(@"Images\Output\out.jpg");
}

Graphics:

using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var watermark = new Bitmap(@"Images\watermark.png"))
{
//Make the watermark semitransparent.
watermark.Channels.ScaleAlpha(0.8F);

//Watermark an image.
using (var graphics = bitmap.GetGraphics())
{
graphics.DrawImage(watermark, 10, bitmap.Height - watermark.Height - 40, CombineMode.Alpha);
//Save the resulting image
bitmap.Save(@"Images\Output\out.jpg");
}
}

Saving Image from a picture box ,image was drawn by graphics object

A couple of problems.

First, CreateGraphics is a temporary drawing surface, not suitable for saving anything. I suspect you want to actually create a new image and display it in the second PictureBox:

Bitmap newBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(newBitmap)) {
g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
}
pictureBox2.Image = newBitmap;

Secondly, use the Path.Combine function to create your file string:

string file = Path.Combine(new string[] { Directory.GetCurrentDirectory(), "Debug", "patch1.jpg" });
newBitmap.Save(file, ImageFormat.Jpeg);

That path has to exist or else the Save method will throw a GDI+ exception.

Save the image from the picturebox

This is not a full answer at this stage but it includes a significant amount of code, so I'll post as an answer and then either update or delete as required.

I just tried this code and all four saves worked, even though I thought that some may not:

Imports System.IO

Public Class Form1

Private ReadOnly folderPath As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyPictures, "Test")

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox1.Image = Image.FromFile(Path.Combine(folderPath, "Picture1.png"))

Using fs = File.OpenRead(Path.Combine(folderPath, "Picture2.png"))
PictureBox2.Image = Image.FromStream(fs)
End Using

PictureBox3.ImageLocation = Path.Combine(folderPath, "Picture3.png")

PictureBox4.Load(Path.Combine(folderPath, "Picture4.png"))
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
PictureBox1.Image.Save(Path.Combine(folderPath, "Output1.png"))
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
PictureBox2.Image.Save(Path.Combine(folderPath, "Output2.png"))
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Try
PictureBox3.Image.Save(Path.Combine(folderPath, "Output3.png"))
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Try
PictureBox4.Image.Save(Path.Combine(folderPath, "Output4.png"))
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub

End Class

Can you please test that with your own images in your own folder and see if you get the same result?

Note that the Image.FromFile option locks the file until you dispose the Image. I have seen instances in the past where trying to save an Image created using Image.FromStream generates the error message you mentioned because it can't access the stream again later. Image.FromFile avoids that but has its own downsides.

My code that uses a FileStream directly is simpler and, therefore, better than yours but setting ImageLocation or calling Load is simpler again and thus better again. I suggest that you use one of those two options and see whether you still have an issue.



Related Topics



Leave a reply



Submit