private void setResizeMetdhod(ResizeMethod.Method method) { int index = -1; log.debug("Changing resize method to {0}", method.ToString()); foreach (object obj in resizeMethodCmBx.Items) { index += 1; ResizeMethod m = (ResizeMethod)obj; if (m.method == method) { resizeMethodCmBx.SelectedIndex = index; return; } } throw new Exception("Internal error, method not found on available list on GUI"); }
public Profile(string name, ResizeMethod.Method method, int width, int height, bool is_fixed) { this.name = name; this.method = method; this.width = width; this.height = height; this.is_fixed = is_fixed; // To indicate which profiles can be removed and which are fixed on the application }
void resize_image(string input_path, string output_folder, ResizeMethod.Method method, int box_width, int box_height) { log.debug("Resizing image"); log.debug(" Source.......: " + input_path); log.debug(" Output folder: " + output_folder); log.debug(" Method.......: " + method); log.debug(" Box Width....: " + box_width); log.debug(" Box Height...: " + box_height); Image loaded_image = Image.FromFile(input_path); Image output_image; int source_width = loaded_image.Width; int source_height = loaded_image.Height; int source_x = -1; int source_y = -1; int destination_x = -1; int destination_y = -1; int destination_width = -1; int destination_height = -1; source_width = loaded_image.Width; source_height = loaded_image.Height; if (method == ResizeMethod.Method.stretch) { source_x = 0; source_y = 0; destination_x = 0; destination_y = 0; destination_height = box_height < source_height ? box_height : source_height; destination_width = box_width < source_width ? box_width : source_width; } else if (method == ResizeMethod.Method.fit_on_box) { double boxRatio = Convert.ToDouble(box_width) / Convert.ToDouble(box_height); double sourceRatio = Convert.ToDouble(source_width) / Convert.ToDouble(source_height); if (sourceRatio > boxRatio) { // original ratio is larger than destination ratio. Thus, image width will match box's width and height will be smaller destination_width = box_width < source_width ? box_width : source_width; destination_height = (int)(destination_width / sourceRatio); } else { destination_height = box_height < source_height ? box_height : source_height; destination_width = (int)(sourceRatio * destination_height); } source_x = 0; source_y = 0; destination_x = 0; destination_y = 0; } else if (method == ResizeMethod.Method.cut_excess) { double boxRatio = Convert.ToDouble(box_width) / Convert.ToDouble(box_height); double sourceRatio = Convert.ToDouble(source_width) / Convert.ToDouble(source_height); log.debug("Resize intermediate parameters:"); log.debug(" Box Ratio........: " + boxRatio); log.debug(" Source Ratio.....: " + sourceRatio); if (source_width < destination_width && source_height < destination_height) { log.debug("Source image fits on output box, not cutting any side of the image"); source_x = 0; source_y = 0; destination_x = 0; destination_y = 0; destination_height = source_height; destination_width = source_width; } else if (sourceRatio > boxRatio) { // original image is wider than the box target and it's ratio is also wider. // Then, need to reduce image height in order to // 1) don't stretch image // 2) Meet box size destination_width = box_width; double taken_width = -1.0; if (source_height > box_height) { destination_height = box_height; taken_width = boxRatio * Convert.ToDouble(source_height); } else { destination_height = source_height; taken_width = box_width; } int excess_side = (int)((source_width - taken_width) / 2); log.debug(" Taken width......: " + taken_width); log.debug(" Excess side......: " + excess_side); source_width = (int)taken_width; source_y = 0; source_x = (int)excess_side; destination_x = 0; destination_y = 0; } else { // original image is higher than the box target and it's ratio is also higher. // Then, need to reduce image height in order to // 1) don't stretch image // 2) Meet box size destination_height = box_height; double taken_height = -1.0; if (source_width > box_width) { destination_width = box_width; taken_height = Convert.ToDouble(source_width) / boxRatio; } else { destination_width = source_width; taken_height = box_height; } int excess_top = (int)((source_height - taken_height) / 2); log.debug(" Taken height.....: " + taken_height); log.debug(" Excess top.......: " + excess_top); source_height = (int)taken_height; source_y = excess_top; source_x = 0; destination_x = 0; destination_y = 0; } } else if (method == ResizeMethod.Method.add_fill) { double boxRatio = Convert.ToDouble(box_width) / Convert.ToDouble(box_height); double sourceRatio = Convert.ToDouble(source_width) / Convert.ToDouble(source_height); if (sourceRatio > boxRatio) { // original ratio is larger than destination ratio. Thus, image width will match box's width and height will be smaller destination_width = box_width < source_width ? box_width : source_width; destination_height = (int)(destination_width / sourceRatio); } else { destination_height = box_height < source_height ? box_height : source_height; destination_width = (int)(sourceRatio * destination_height); } source_x = 0; source_y = 0; destination_x = 0; destination_y = 0; } else { log.error("Unsupported resize method: {0}", method); throw new Exception(string.Format("Internal error, unsupported resize method: {0}", method)); return; } log.debug("Resize caulculated parameters:"); log.debug(" Source Width......: " + source_width); log.debug(" Source Height.....: " + source_height); log.debug(" Source X..........: " + source_x); log.debug(" Source Y..........: " + source_y); log.debug(" Destination X.....: " + destination_x); log.debug(" Destination Y.....: " + destination_y); log.debug(" Destination Width.: " + destination_width); log.debug(" Destination Height: " + destination_height); Bitmap bmPhoto = new Bitmap(destination_width, destination_height, PixelFormat.Format24bppRgb); bmPhoto.SetResolution(loaded_image.HorizontalResolution, loaded_image.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(loaded_image, new Rectangle(destination_x, destination_y, destination_width, destination_height), new Rectangle(source_x, source_y, source_width, source_height), GraphicsUnit.Pixel); grPhoto.Dispose(); output_image = bmPhoto; //List<ImageCodecInfo> ici = new List<ImageCodecInfo>(); //ici.AddRange(ImageCodecInfo.GetImageEncoders()); log.warning("Missing to add encoding compression level into the output format"); //ici.MimeType output_image.Save(Path.Combine(output_folder, Path.GetFileName(input_path)), ImageFormat.Jpeg); loaded_image.Dispose(); output_image.Dispose(); }