Click to See Complete Forum and Search --> : [RESOLVED] Overwrite and image
vandel212
September 3rd, 2009, 10:14 AM
I'm trying to save an image over a file with the same file name. When it attempts to do this is bombs out because it says either it's a general GSI+ error or the process is already in use. This makes perfect sense why it would throw those errors, but how can I overwrite the file so that it doesn't give me this problem. I've tried Opening a file stream around it then closeing it. That didn't work. I also tried useing the *bitmap variable*.Dispose. That didn't seem to do anything. Is there something else I could try? or perhaps something I'm doing wrong?
rliq
September 3rd, 2009, 11:20 PM
Vandel, Can you post a snippet of your code, so we can see exactly what you are trying to do? I know sometimes it is hard to describe a problem and an example if usually better. Make sure you use the [C O D E] [/ C O D E] tags to surround your example.
vandel212
September 4th, 2009, 01:00 PM
Ok here it is, I run into the problem when I try to click on something other than the form (ex. a different application in the task bar). I would like it to save but when I get to the bmpPicture.Save, it bombs out on me.
For my code See next reply (Sorry for double post, it had to be done though).
Thanks
vandel212
September 8th, 2009, 08:10 AM
Not a big fan of double posting but I don't know of a different way to refresh this to the top of the forum. If there is a better way please let me know. I'm not sure if the first bit of code is enough to figure out what the problem is. So here is everything.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
namespace Red_Lining_Application
{
public partial class Form1 : Form
{
bool blnFirstTimeLoad = true;
private bool mouseDown = false;
private Point lastPoint = Point.Empty;
private string color = "black";
private Graphics g;
private Pen p;
private static Bitmap bmpPicture;
private static Graphics gfxScreenShot;
string FilePath = "C:\\Documents and Settings\\" + Environment.UserName + "\\My Documents\\My Pictures\\Screen_Shot.bmp";
public Form1()
{
/*
// Set the bitmap object to the size of the screen
bmpPicture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap
gfxScreenShot = Graphics.FromImage(bmpPicture);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenShot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y + 25, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
// Save the screenshot to the specified path that the user has chosen
bmpPicture.Save(FilePath, ImageFormat.Bmp);
int intFormSizeVert;
int intFormSizeHorz;
Bitmap picture = (Bitmap)Image.FromFile(FilePath);
intFormSizeHorz = picture.Width;
intFormSizeVert = picture.Height;
Size = new Size(intFormSizeHorz, intFormSizeVert);
this.BackgroundImage = picture;
g = CreateGraphics();
p = new Pen(Color.FromName(color), 3);
*/
InitializeComponent();
}
protected override void OnMouseDown(MouseEventArgs e)
{
mouseDown = true;
if (e.Button == MouseButtons.Right)
{
p = new Pen(Color.Red, 3);
}
if (e.Button == MouseButtons.Left)
{
p = new Pen(Color.Black, 3);
}
}
private void SaveFile()
{
// Set the bitmap object to the size of the screen
bmpPicture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap
gfxScreenShot = Graphics.FromImage(bmpPicture);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenShot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y + 25, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
// Save the screenshot to the specified path that the user has chosen
bmpPicture.Save(FilePath, ImageFormat.Bmp); //****THIS IS WHERE THE ERROR IS THROWN****
}
protected override void OnMouseUp(MouseEventArgs e)
{
mouseDown = false;
SaveFile();
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (blnFirstTimeLoad == false)
{
if (lastPoint.Equals(Point.Empty)) lastPoint = new Point(e.X, e.Y);
if (mouseDown)
{
Point pMousePos = new Point(e.X, e.Y);
g.DrawLine(p, pMousePos, lastPoint);
}
lastPoint = new Point(e.X, e.Y);
}
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void printScreenToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFile();
int intFormSizeVert;
int intFormSizeHorz;
Bitmap picture = (Bitmap)Image.FromFile(FilePath);
intFormSizeHorz = picture.Width;
intFormSizeVert = picture.Height;
Size = new Size(intFormSizeHorz, intFormSizeVert);
this.BackgroundImage = picture;
g = CreateGraphics();
p = new Pen(Color.FromName(color), 3);
blnFirstTimeLoad = false;
this.Activate();
this.Opacity = 100;
}
Arjay
September 8th, 2009, 04:49 PM
Why did you declare the gfxScreenShot and bmpPicture fields as static?
vandel212
September 9th, 2009, 05:58 AM
To be honest, I'm still learning about GDI+ and that is what I found online on drawing graphics on a form. What would you use instead?
**EDIT**
I think I may have found the line causing all of my problems. After saving it displays the file and in the code that causes it to display the picture there is the line
Bitmap picture = (Bitmap)Image.FromFile(FilePath);
when this line runs I think it causes the file to get "locked" (i'm using this term loosly because it might not be getting locked in the C# sense) and doesn't allow the file.Dispose(); to work it's magic. The problem now is that I don't know how to fix it.
vandel212
September 11th, 2009, 09:56 AM
Well, I think I figured it out I changed the line that was giving me problems to
Bitmap picture = (bmpPicture);
That fixed the problem. So I'll consider this resolved. hopefully this will help somone in the future.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.