Sign in to follow this  
PM-Heavy

C# Overwrite Image

Recommended Posts

I'm writing a C# application that uses the DSO file to add metadata to images. I can read the data, and use a save dialog to change the saved image name. However, when I try to overwrite an image, my try-catch block throws an exception. I'm using WinForms and an open dialog to load the image. Does that cause the image to be read only? I'm not actually changing the image, just the metadata.

Share this post


Link to post
Share on other sites
Here's the whole file. I'm calling the function addMetadata(), and the exception is thrown during saveToolStripMenuItem_Click(object sender, EventArgs e).


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.IO;
using DSOFile;
namespace pictureOrganizer
{
public partial class Form1 : Form
{
string loadedImageName;
string loadedImageFullName;

public Form1()
{
InitializeComponent();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}

public void getMetadata()
{
FileInfo fileInfo = new FileInfo(loadedImageFullName);
OleDocumentPropertiesClass oleDoc = new OleDocumentPropertiesClass();

oleDoc.Open(fileInfo.FullName, false, dsoFileOpenOptions.dsoOptionDefault);

nameTextBox.Text = fileInfo.Name;
tagsTextBox.Text = oleDoc.SummaryProperties.Keywords;
commentsTextBox.Text = oleDoc.SummaryProperties.Comments;
}

public void addMetadata() //this is the function I'm calling that throws the exception
{
FileInfo fileInfo = new FileInfo(loadedImageFullName);
OleDocumentPropertiesClass oleDoc = new OleDocumentPropertiesClass();

oleDoc.Open(fileInfo.FullName, false, dsoFileOpenOptions.dsoOptionDefault);

string tags = tagsTextBox.Text;

if (!oleDoc.IsReadOnly)
{
string tagsProper = tags.Replace('\n', ';').Replace(',', ';');

oleDoc.SummaryProperties.Comments = commentsTextBox.Text;
oleDoc.SummaryProperties.Keywords = tagsProper;
oleDoc.SummaryProperties.Title = nameTextBox.Text;
}

if (oleDoc.IsDirty)
{
oleDoc.Save();
}
}

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
addMetadata();
}
catch
{
throw new ApplicationException("Could not save image"); //here's the exception thrown
}
}

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (saveDialog.ShowDialog() == DialogResult.OK)
{
loadedImageFullName = saveDialog.FileName;
loadedImageName = saveDialog.FileName;
pictureBox1.Image.Save(loadedImageFullName);
addMetadata();
}
}
catch (Exception)
{
throw new ApplicationException("Could not save the image.");
}
}

private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (openDialog.ShowDialog() == DialogResult.OK)
{
loadedImageFullName = openDialog.FileName;
pictureBox1.Image = new Bitmap(openDialog.FileName);
}

openDialog.Dispose();
getMetadata();
}
catch (Exception)
{
throw new ApplicationException("Could not load the image.");
}
}

private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("This feature not yet implemented.");
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("This feature not yet implemente.");
}

private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(loadedImageFullName);
}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void previous_Click(object sender, EventArgs e)
{

}

private void next_Click(object sender, EventArgs e)
{

}

private void nameTextBox_TextChanged(object sender, EventArgs e)
{

}

private void tagsTextBox_TextChanged(object sender, EventArgs e)
{

}

private void commentsTextBox_TextChanged(object sender, EventArgs e)
{

}
}
}

Share this post


Link to post
Share on other sites
Don't swallow exceptions like that. You end up losing valuable information. If you can't act on an exception, don't catch it. Once you take that try ... catch out, the compiler should break when the real exception is thrown and you should be able to look at the information it contains to help solve the problem.

Share this post


Link to post
Share on other sites
I just got rid of the try-catch statements and the new exception is thrown here
		public void addMetadata()
{
FileInfo fileInfo = new FileInfo(loadedImageFullName);
OleDocumentPropertiesClass oleDoc = new OleDocumentPropertiesClass();

oleDoc.Open(fileInfo.FullName, false, dsoFileOpenOptions.dsoOptionDefault);

string tags = tagsTextBox.Text;

if (!oleDoc.IsReadOnly)
{
string tagsProper = tags.Replace('\n', ';').Replace(',', ';');

oleDoc.SummaryProperties.Comments = commentsTextBox.Text;
oleDoc.SummaryProperties.Keywords = tagsProper;
oleDoc.SummaryProperties.Title = nameTextBox.Text;
}

if (oleDoc.IsDirty)
{
oleDoc.Save(); //exception thrown here
}
}


The exception is "The process cannot access the file because it is being used by another process. (Exception from HRESULT: 0x80070020)". I don't have the image opened with any other programs so I'm not sure what else could be using it.

Share this post


Link to post
Share on other sites
Your program, perhaps? If the interface you're using to access the file in your code implements IDisposable, make sure you are calling Dispose() on the object (or employing a using block) when you're done. Otherwise the object will not be disposed until finalization, and may keep an unmanaged file handle open, preventing further IO.

Share this post


Link to post
Share on other sites
I'm not sure exactly what you mean. I'm using an OpenFileDialog to load the image and calling Dispose() on it after the image is loaded.

Also, what's a using block? I don't think I've heard of that before.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this