Lutinore
Messages postés
3246
Date d'inscription
lundi 25 avril 2005
Statut
Membre
Dernière intervention
27 octobre 2012
41
10 avril 2007 à 02:53
Sa méthode marche super bien, je l'ai un peu optimisée et j'obtiens 0 secondes et des poussières pour 100 images en 640x480 réduit en 80x80
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
Button b = new Button( );
b.Parent = this;
b.Text = "Start";
b.Click += delegate
{
string[ ] paths = Directory.GetFiles( @"C:\Users\Mike\Desktop\Angel" , "*.jpg" );
Stopwatch sw = new Stopwatch( );
sw.Reset( ); sw.Start( );
Bitmap[ ] thumbnails = GetThumbnails( paths, 80, 80 );
sw.Stop( );
MessageBox.Show( string.Format( "Time: {0}\nCount: {1}", sw.Elapsed, thumbnails.Length ) );
};
}
public Bitmap[ ] GetThumbnails( string[ ] paths, int width, int height )
{
int len = paths.Length;
Bitmap[ ] thumbnails = new Bitmap[ len ];
for( int i = 0; i < len; i++ )
{
try
{
Bitmap bmp = LoadBitmapFromFile( paths[ i ] );
thumbnails[ i ] = new Bitmap( width, height, PixelFormat.Format24bppRgb );
using ( Graphics g = Graphics.FromImage( thumbnails[ i ] ) )
{
//g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage( bmp, new Rectangle( 0, 0, width, height ) );
}
bmp.Dispose( );
}
catch
{
}
}
return thumbnails;
}
public enum GpStatus
{
Ok = 0,
GenericError = 1,
InvalidParameter = 2,
OutOfMemory = 3,
ObjectBusy = 4,
InsufficientBuffer = 5,
NotImplemented = 6,
Win32Error = 7,
WrongState = 8,
Aborted = 9,
FileNotFound = 10,
ValueOverflow = 11,
AccessDenied = 12,
UnknownImageFormat = 13,
FontFamilyNotFound = 14,
FontStyleNotFound = 15,
NotTrueTypeFont = 16,
UnsupportedGdiplusVersion = 17,
GdiplusNotInitialized = 18,
PropertyNotFound = 19,
PropertyNotSupported = 20,
ProfileNotFound = 21
}
[ DllImport( "gdiplus.dll", CharSet = CharSet.Unicode ), SuppressUnmanagedCodeSecurity ]
private static extern GpStatus GdipLoadImageFromFile( string filename, out IntPtr image );
private readonly MethodInfo BitmapFromGdiPLus =
typeof( Bitmap ).GetMethod( "FromGDIplus", BindingFlags.NonPublic | BindingFlags.Static );
public Bitmap LoadBitmapFromFile( string filename )
{
IntPtr gpImage = IntPtr.Zero;
GpStatus gpStatus = GdipLoadImageFromFile( filename, out gpImage );
if ( gpStatus != GpStatus.Ok )
throw new ApplicationException( string.Format( "GdiPlus error: {0}.", gpStatus ) );
return ( Bitmap )BitmapFromGdiPLus.Invoke
(
null,
BindingFlags.Default,
null,
new object[ ] { gpImage },
null
);
}
}