Monday, March 01, 2004 - Posts

Image Compression

Ok so after I got the cropping of a image done (see my log Cropping image) and had the “A generic error occurred in GDI+.” Error. By using:
using(Bitmap bitmap = new Bitmap(imageRigth))
{
 bitmap.Save(MapPath(fileName), imageRigth.RawFormat);
}

After that I was just uploading some picture for some more testing but the thing I was seeing was that the picture’s where 300 kb for just a 250x300 picture witch is large So I needed to get the stuff compressed here is how to do it.

First why is the picture saved now so big? Very easy it is saved as RawFormat (bmp).
How to save a compressed version this can be done like this:
public void Save(Stream, ImageCodecInfo, EncoderParameters);
So we have the stream witch is the image now for the “ImageCodecInfo”.
For the ImageCodecInfo I have the following function:
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
 ImageCodecInfo[] encoders;
 encoders = ImageCodecInfo.GetImageEncoders();
 for(int j = 0; j < encoders.Length; ++j)
 {
  if(encoders[j].MimeType == mimeType)
   return encoders[j];
 }
 return null;
}

You could get the jpg codec with "image/jpeg" as mimeType, “image/gif” for gif , “image/tiff” for tif or “image/png” for png.
That’s really it I never got the hang on what it exactly dues only that it go’s for the codec method that you put in.
Now for the EncoderParameters part this is the picture encoder , there can be multiple encoders in a “EncoderParameters“. I have the following function that I use for the encoder:
private static EncoderParameters Encode()
{
 EncoderParameters myEncoderParameters = new EncoderParameters();
 EncoderParameter myEncoderParameter = new EncoderParameter(Encoder.Compression,(long)EncoderValue.CompressionLZW);
 myEncoderParameters.Param[0] = myEncoderParameter;
 return myEncoderParameters;
}

It is also possible to get more then one encoder’s but why to do this is mostly a ? for me.

N00B DataAdapter Mistake

So I wanted to insert some info into a table and I was using:
sqlDataAdapterCategoryList.InsertCommand.Parameters["Titel"].Value = txt_titel;
sqlDataAdapterCategoryList.InsertCommand.Parameters["Description"].Value = txt_description;
sqlDataAdapterCategoryList.InsertCommand.ExecuteNonQuery();

And this worked perfect but when I wanted to get the info from the select that is included into the generated insert statement. So after trying to fill the dataset after the insert command execution I could get the entire last put in using:
ds.Categories[(ds.Categories.Count-1)]
Where ds is the dataset. But this seemed to be a wasted of data traffic so I needed something better so after trying some stuff for 30 min I went to my neighbor and hey showed me this:
DataSetCategory ds = new DataSetCategory();
Category db = new Category();
ds.Categories.AddCategoriesRow(Int32.Parse(ddl_parent.SelectedValue),txt_titel.Text,txt_description.Text);
sqlDataAdapterCategoryList.Update(ds);

So now I could insert the information and get the information using:
ds.Categories[0]
Without wasting resources or using a datareader or scalar