Multiple image.save() when changing quality

G

Guest

i am writing a class that visual demonstrates the result of changing the quality of a jpeg. the intention is to do this by saving the file and a selected quality, then reloading it to get an idea of what the image looks like, and how big it is. the quality is selected using a trackBar1. the MouseUp event of trackBar1 triggers this method

public void updatePanel()
string file = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\jpg.temp"

//sav
ImageCodecInfo ico = GraphicsTools.getEncoderInfo("image/jpeg")
Encoder qualityEncoder = Encoder.Quality
EncoderParameter ratio = new EncoderParameter(qualityEncoder, quality)
EncoderParameters p = new EncoderParameters(1)
p.Param[0] = ratio

try
bob.Save(file, ico, p)
}catch(System.Runtime.InteropServices.ExternalException ex)
Console.WriteLine(ex.Message)


//loa
panel2.BackgroundImage = Image.FromFile(file)
FileInfo fi = new FileInfo(file)
textBox1.Text = ((int)(fi.Length/1024)).ToString() + "KB"


'bob.Save(file, ico, p)' throws an 'ExternalException', but only from the second time this method is called - never the first. i can put multiple image.save calls in this method, and have them all work the first time this method is called. how do i need to change this code to allow me to save multiple times, at any quality, to the same location?
 
G

Guest

addendum

i solved this problem by saving to 'System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\temp\\" + quality.ToString() + ".temp";'

using a different filename for each quality got around the problem, but it created a lot of tmepory files that need deleting. there has to be a better way
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top