Página 1 de 3

Dll: ImageConvertResize

Publicado: 05 May 2011 18:37
por rafaxplayer
Hola compañeros , me he animado hacer una dll en c# después de ver el tutorial de pabloko ( gracias ) , la dll sirve para redimensionar y convertir de un formato a otro imagenes , la redimension mantiene una relaccion tamaño aspecto , osea que rstringe proporcines para no deformar la imagen , la llamada solo tiene una funcion:

ResizeConvertImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, string format, int OnlyResizeIfWider)

string OriginalFile = archivo de salida
string NewFile = Archivo de destino , debemos proporcionar ruta completa nombre del archivo y nueva extensión en caso de cambio.
int NewWidth = Nuevo tamaño en anchura
int MaxHeight = la altura dependerá del ancho , podemos fijar un maximo pero entoces cambiara newwidth al valor que no deforme la imagen.
string format = Formato de salida de la imagen , formatos soportados : "Bmp","Emf","Exif","Gif","Ico","Jpeg","Png","Tiff","Wmf".
int OnlyResizeIfWider= valor 1 = solo redimensiona si es para mayor , 0 = redimensiona siempre.

Codigo fuente de la dll:
using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;

namespace ImageConverResizer
{
internal static class UnmanagedExports
{
[DllExport("ResizeConvertImage", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static void ResizeConvertImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, string format, int OnlyResizeIfWider)
{
System.Drawing.Image Image = System.Drawing.Image.FromFile(OriginalFile);

// Prevent using images internal thumbnail
Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

if (OnlyResizeIfWider == 1)
{
if (Image.Width <= NewWidth)
{
NewWidth = Image.Width;
}
}

int NewHeight = Image.Height * NewWidth /Image.Width;
if (NewHeight > MaxHeight)
{
// Resize with height instead
NewWidth = Image.Width * MaxHeight / Image.Height;
NewHeight = MaxHeight;
}

System.Drawing.Image newImage = Image.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
var formatFile = System.Drawing.Imaging.ImageFormat.Bmp;
if (format == "Bmp") { formatFile = System.Drawing.Imaging.ImageFormat.Bmp; }
else if (format == "Emf") { formatFile = System.Drawing.Imaging.ImageFormat.Emf; }
else if (format == "Exif") { formatFile = System.Drawing.Imaging.ImageFormat.Exif; }
else if (format == "Gif") { formatFile = System.Drawing.Imaging.ImageFormat.Gif; }
else if (format == "Ico") { formatFile = System.Drawing.Imaging.ImageFormat.Icon; }
else if (format == "Jpeg") { formatFile = System.Drawing.Imaging.ImageFormat.Jpeg; }
else if (format == "Png") { formatFile = System.Drawing.Imaging.ImageFormat.Png; }
else if (format == "Tiff") { formatFile = System.Drawing.Imaging.ImageFormat.Tiff; }
else if (format == "Wmf") { formatFile = System.Drawing.Imaging.ImageFormat.Wmf; }
// Clear handle to original file so that we can overwrite it if necessary
Image.Dispose();

// Save resized picture
newImage.Save(NewFile,formatFile);
}

}
}


Es la primera vez que hago algo con C# y gracias a que pabloko nos lo pone fácil , así que supongo que el código tendrá mejoras y errores para parar un carro ( alguna vez hay que empezar) , espero vuestros comentarios y sugerencias , saludos

Descarga:

HIDE: ON
Hidebb Message Hidden Description

Re: DLL: ImageConvertResize

Publicado: 05 May 2011 18:52
por Ceone
jejejejeje ya te dige que era muy facil ;) genial trabajo!

Re: DLL: ImageConvertResize

Publicado: 05 May 2011 19:51
por patch
There is one thing you did forget you are only changing he image w/h the file as not really been changed tho as you not also included resolution maybe that could be a update for the future :)

Re: DLL: ImageConvertResize

Publicado: 05 May 2011 21:46
por rafaxplayer
patch escribió:There is one thing you did forget you are only changing he image w/h the file as not really been changed tho as you not also included resolution maybe that could be a update for the future :)
My experience in C # is 0, if you fix some script I'd like to see it to improve the dll, Greetings

Re: DLL: ImageConvertResize

Publicado: 05 May 2011 22:28
por Agotaras123
excelente trabajo amigo sin duda todos se están animando a hacer dll haber cuando me entra a mi la fiebre de las dll, por los momentos solo tengo la de usarlas nada mas ejejejje.

La usare en mi proyecto haber como anda y si encuentro algo interesante o no interesante te avisare en cuanto la use.

Re: Dll: ImageConvertResize

Publicado: 07 May 2011 15:29
por Pabloko
using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;

namespace ImageConverResizer
{
internal static class UnmanagedExports
{
[DllExport("ResizeConvertImage", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static void ResizeConvertImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, string format, int OnlyResizeIfWider)
{
System.Drawing.Image Image = System.Drawing.Image.FromFile(OriginalFile);

// Prevent using images internal thumbnail
Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

if (OnlyResizeIfWider == 1)
{
if (Image.Width <= NewWidth)
{
NewWidth = Image.Width;
}
}

int NewHeight = Image.Height * NewWidth / Image.Width;
if (NewHeight > MaxHeight)
{
// Resize with height instead
NewWidth = Image.Width * MaxHeight / Image.Height;
NewHeight = MaxHeight;
}

System.Drawing.Image newImage = Image.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

// Clear handle to original file so that we can overwrite it if necessary
Image.Dispose();

// Save resized picture
newImage.Save(NewFile, formatt(format));
}

private static System.Drawing.Imaging.ImageFormat formatt(string e)
{
switch (e)
{
case "Bmp":
return System.Drawing.Imaging.ImageFormat.Bmp;
case "Emf":
return System.Drawing.Imaging.ImageFormat.Emf;
case "Exif":
return System.Drawing.Imaging.ImageFormat.Exif;
case "Gif":
return System.Drawing.Imaging.ImageFormat.Gif;
case "Icon":
return System.Drawing.Imaging.ImageFormat.Icon;
case "Jpeg":
return System.Drawing.Imaging.ImageFormat.Jpeg;
case "MemoryBmp":
return System.Drawing.Imaging.ImageFormat.MemoryBmp;
case "Png":
return System.Drawing.Imaging.ImageFormat.Png;
case "Tiff":
return System.Drawing.Imaging.ImageFormat.Tiff;
case "Wmf":
return System.Drawing.Imaging.ImageFormat.Wmf;
default:
return System.Drawing.Imaging.ImageFormat.Jpeg;
}
}

}
}


problem solved

Re: Dll: ImageConvertResize

Publicado: 07 May 2011 20:32
por abood1987
Special work

:yes: :yes: :yes:

Re: Dll: ImageConvertResize

Publicado: 08 May 2011 08:59
por xxsolracxx
grcias<div>
</div>

Re: Dll: ImageConvertResize

Publicado: 08 May 2011 22:24
por rafaxplayer
Pabloko escribió:
using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;

namespace ImageConverResizer
{
internal static class UnmanagedExports
{
[DllExport("ResizeConvertImage", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static void ResizeConvertImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, string format, int OnlyResizeIfWider)
{
System.Drawing.Image Image = System.Drawing.Image.FromFile(OriginalFile);

// Prevent using images internal thumbnail
Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

if (OnlyResizeIfWider == 1)
{
if (Image.Width <= NewWidth)
{
NewWidth = Image.Width;
}
}

int NewHeight = Image.Height * NewWidth / Image.Width;
if (NewHeight > MaxHeight)
{
// Resize with height instead
NewWidth = Image.Width * MaxHeight / Image.Height;
NewHeight = MaxHeight;
}

System.Drawing.Image newImage = Image.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

// Clear handle to original file so that we can overwrite it if necessary
Image.Dispose();

// Save resized picture
newImage.Save(NewFile, formatt(format));
}

private static System.Drawing.Imaging.ImageFormat formatt(string e)
{
switch (e)
{
case "Bmp":
return System.Drawing.Imaging.ImageFormat.Bmp;
case "Emf":
return System.Drawing.Imaging.ImageFormat.Emf;
case "Exif":
return System.Drawing.Imaging.ImageFormat.Exif;
case "Gif":
return System.Drawing.Imaging.ImageFormat.Gif;
case "Icon":
return System.Drawing.Imaging.ImageFormat.Icon;
case "Jpeg":
return System.Drawing.Imaging.ImageFormat.Jpeg;
case "MemoryBmp":
return System.Drawing.Imaging.ImageFormat.MemoryBmp;
case "Png":
return System.Drawing.Imaging.ImageFormat.Png;
case "Tiff":
return System.Drawing.Imaging.ImageFormat.Tiff;
case "Wmf":
return System.Drawing.Imaging.ImageFormat.Wmf;
default:
return System.Drawing.Imaging.ImageFormat.Jpeg;
}
}

}
}


problem solved
Gracias makina , actualizo la dll ;)

Re: Dll: ImageConvertResize

Publicado: 08 May 2011 23:44
por patch
I am also bad at C# so sadly lua is my language and I still not as good as some but I do try hard :)

Re: Dll: ImageConvertResize

Publicado: 21 May 2011 15:34
por carsonzillo
sexy enchufe XD

Re: Dll: ImageConvertResize

Publicado: 23 May 2011 14:17
por dangngocnguyenit
oh. thanks

Re: Dll: ImageConvertResize

Publicado: 09 Jun 2011 16:11
por ams2010
gracias

Re: Dll: ImageConvertResize

Publicado: 14 Jun 2011 04:33
por xxsolracxx
excelente dll, pero que te parece si agregas una opción mas una de rencionar la imagen estirándola totalmente sin que conserve su aspecto original

Re: Dll: ImageConvertResize

Publicado: 16 Ago 2011 17:03
por nghethihieu
thank

Re: Dll: ImageConvertResize

Publicado: 08 Nov 2011 22:15
por milano88
thanks

Re: Dll: ImageConvertResize

Publicado: 22 Feb 2012 16:41
por bariza-dz
thanks

Re: Dll: ImageConvertResize

Publicado: 22 Feb 2012 21:33
por mecivic
Thank you

Re: Dll: ImageConvertResize

Publicado: 22 Feb 2012 23:13
por timveer
Gracias!

Re: Dll: ImageConvertResize

Publicado: 27 Feb 2012 17:50
por acf
good functions