CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2009
    Posts
    112

    [RESOLVED] Overwrite and image

    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?

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Overwrite and image

    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.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Feb 2009
    Posts
    112

    Re: Overwrite and image

    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
    Last edited by vandel212; September 8th, 2009 at 09:42 AM.

  4. #4
    Join Date
    Feb 2009
    Posts
    112

    Re: Overwrite and image

    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.

    Code:
    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;
            }
    Last edited by vandel212; September 8th, 2009 at 09:45 AM.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Overwrite and image

    Why did you declare the gfxScreenShot and bmpPicture fields as static?

  6. #6
    Join Date
    Feb 2009
    Posts
    112

    Re: Overwrite and image

    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.
    Last edited by vandel212; September 9th, 2009 at 09:21 AM.

  7. #7
    Join Date
    Feb 2009
    Posts
    112

    Re: Overwrite and image

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured