5 Replies - 1312 Views - Last Post: 07 March 2010 - 02:48 AM Rate Topic: -----

#1 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Saving several images at once

Posted 04 March 2010 - 03:19 PM

Hi,

I am developing a Zoo Database System and I have developed functionality to allow a user to write the title to an image and I can apply this to multiple images but i want to do in an efficient way.

Currently i write the title to multiple images by allowing the user to specify a directory in a textbox, creating the image objects and then outputing each image to chosen directory but I want to update the images property info where they're currently located.

So say i have 10 images in C:\SomeFolder\, if i select Save button on my form,the property information of the images that are located at in that folder should be updated.

I can save a single image to a chosen location by using a SaveFileDialog control with the following code:

private void saveJpeg(string path, Bitmap img, long quality)
{
   // Encoder parameter for image quality
   EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);

   // Jpeg image codec
   ImageCodecInfo jpegCodec = this.getEncoderInfo("image/jpeg");

   if(jpegCodec == null)
      return;

   EncoderParameters encoderParams = new EncoderParameters(1);
   encoderParams.Param[0] = qualityParam;

   img.Save(path, jpegCodec, encoderParams);
}

private ImageCodecInfo getEncoderInfo(string mimeType)
{
   // Get image codecs for all image formats
   ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

   //Loop through codec to get correct one
   for (int i = 0; i < codecs.Length; i++)
      if (codecs[i].MimeType == mimeType)
         return codecs[i];
   return null;
}



I would appreciate if somebody could advise please on the above.

Thanks

This post has been edited by MarmiteX1: 05 March 2010 - 12:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Saving several images at once

#2 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Re: Saving several images at once

Posted 05 March 2010 - 12:49 PM

*Update*

I dont know whether i should post it here or in a seperate post but anyway here i go:

I have had a go at it and does work to an extent but a problem occurs when you try to "save"/ write the images back to the folder they were originally located in.

The message i get is the following:

Quote

A generic error occured in GDI+
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
System.Drawing
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
System.Drawing

Error code: -2147467259

" at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)\r\n
at System.Drawing.Image.Save(String filename, ImageFormat format)\r\n at ZooSystem.ImageViewer.ApplyChanges()
in E:\\ZooSystem\\\ImageViewer.cs:line 520"


I have checked the permissions for each of the images in the folder and they all have FullControl/Read/Write/Modify permissions enabled but the folder has "Read-Only" enabled.

The folder was created on the Internal Hard drive of the computer and I am using Windows Vista Home Premium 32-bit. I am using MS VS 2008 Pro edition which i run in Admin mode.

If i save the images to another folder say one of the Public folders then the above exception is not thrown.

But I would like to save my images to the same folder if possible?

How could i go about doing this? I would appreciate if somebody could help me with the above. I have been trying to fix it for past 2 hours.

Here is the code:

private void ApplyChanges()
{
//check if the destination folder in the textbox is blank in order to save a single image
//using the Save-file dialog feature.
if(txtBoxOut.Text.Equals("")
{
  //here i write the title to the global image by calling a write method

  //show the save file dialog and check if result is OK.
   //call the custom save method 
    if(sfd.ShowDialog()==DialogResult.OK)
    {
        
       this.SaveTheImage(sfd.FileName,this.bmp);
    }
  
}
else if(txtBoxOut.Text != "")
{

   string destFolder = txtBoxOut.text
   foreach(string filePath in filePaths)
   {
      this.bmp = Image.FromFile(filePath);
      string fileName = Path.GetFileName(filePath);
      
     try 
     {

       //write the title to the global image

       //check folder
      if(destFolder.EndsWith("\\")
      {
        //use the inbuilt save method
        this.bmp.Save(destFolder+fileName+ImageFormat.Jpeg);
      }
      else
      {
        this.bmp.Save(destFolder+"\\"+fileName+ImageFormat.Jpeg);
        
      }

     }
     catch(Exception ex)
     {

        Console.WriteLine(ex.message.ToString());
     }

   }
 }
}


Was This Post Helpful? 0
  • +
  • -

#3 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Re: Saving several images at once

Posted 05 March 2010 - 01:01 PM

Hi,

Apologies for the double posting but i just thought it may be best to post this as a seperate thread.

I recently posted a thread about writing a title to more than image at the same time.

Now i have had a go at it and does work to an extent but a problem occurs when you try to "save"/ write the images back to the folder they were originally located in.

The message i get is the following:

Quote

A generic error occured in GDI+
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
System.Drawing
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
System.Drawing

Error code: -2147467259

" at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)\r\n
at System.Drawing.Image.Save(String filename, ImageFormat format)\r\n at ZooSystem.ImageViewer.ApplyChanges()
in E:\\ZooSystem\\\ImageViewer.cs:line 520"


I have checked the permissions for each of the images in the folder and they all have FullControl/Read/Write/Modify permissions enabled but the folder has "Read-Only" enabled.

The folder was created on the Internal Hard drive of the computer and I am using Windows Vista Home Premium 32-bit. I am using MS VS 2008 Pro edition which i run in Admin mode.

If i save the images to another folder say one of the Public folders then the above exception is not thrown.

But I would like to save my images to the same folder if possible?

How could i go about doing this? I would appreciate if somebody could help me with the above. I have been trying to fix it for past 2 hours.

Thanks,

Here is the code:

private void ApplyChanges()
{
//check if the destination folder in the textbox is blank in order to save a single image
//using the Save-file dialog feature.
if(txtBoxOut.Text.Equals("")
{
  //write the title to the global image by calling appropriate method

  //show the save file dialog and check if result is OK.
   //call the custom save method 
    if(sfd.ShowDialog()==DialogResult.OK)
    {
        
       this.SaveTheImage(sfd.FileName,this.bmp);
    }
  
}
else if(txtBoxOut.Text != "")
{

   string destFolder = txtBoxOut.text
   foreach(string filePath in filePaths)
   {
      this.bmp = Image.FromFile(filePath);
      string fileName = Path.GetFileName(filePath);
      
     try 
     {

       //write the title to the global image

       //check folder
      if(destFolder.EndsWith("\\")
      {
        //use the inbuilt save method
        this.bmp.Save(destFolder+fileName+ImageFormat.Jpeg);
      }
      else
      {
        this.bmp.Save(destFolder+"\\"+fileName+ImageFormat.Jpeg);
        
      }

     }
     catch(Exception ex)
     {

        Console.WriteLine(ex.message.ToString());
     }

   }
 }
}


Was This Post Helpful? 0
  • +
  • -

#4 Searock  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 259
  • Joined: 15-September 09

Re: Saving several images at once

Posted 05 March 2010 - 07:31 PM

That's weird.

Can you tell me the full path of the folder.

Try to delete it and then try to create the same folder again
Was This Post Helpful? 0
  • +
  • -

#5 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Re: Saving several images at once

Posted 06 March 2010 - 07:33 AM

View PostSearock, on 05 March 2010 - 06:31 PM, said:

That's weird.

Can you tell me the full path of the folder.

Try to delete it and then try to create the same folder again


The full path is the following:
C:\Users\<username>\Pictures\Animals



I deleted the folder and made a new folder but the problem still persists.

I am still investigating the problem.
Was This Post Helpful? 0
  • +
  • -

#6 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: Saving several images at once

Posted 07 March 2010 - 02:48 AM

Where exactly are the files located on your system?
Still sounds like lack of permission to me, especially since you can save them to another folder without problems.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1