缩略图生成算法

抄的,避免GetThumbNail GIF透明信息丢失的问题,
private unsafe Bitmap GetThumbnail(Bitmap src, int width, int height)
            
{
                
// preserve source-image aspect ratio 

                
float src_t = (float) src.Height/(float) src.Width;

                
if (src_t > ((float) height/(float) width))
                    width 
= (int) (height/src_t);
                
else
                    height 
= (int) (width*src_t);

                
// end of preserve source-image aspect ratio 


                
if (src.PixelFormat == PixelFormat.Format8bppIndexed)
                
{
                    
// do it yourself 

                    Bitmap dst 
= new Bitmap(width, height, src.PixelFormat);
                    dst.Palette 
= src.Palette;

                    BitmapData dstbd 
=
                        dst.LockBits
                            (
                            
new Rectangle(00, dst.Width, dst.Height),
                            ImageLockMode.WriteOnly,
                            dst.PixelFormat
                            );
                    
try
                    
{
                        BitmapData srcbd 
=
                            src.LockBits
                                (
                                
new Rectangle(00, src.Width, src.Height),
                                ImageLockMode.ReadOnly,
                                src.PixelFormat
                                );
                        
try
                        
{
                            
byte* srcp;
                            
byte* dstp;

                            
float m = (float) src.Width/(float) dst.Width;


                            
for (int r = 0; r < dst.Height; ++r)
                            
{
                                dstp 
= (byte*) dstbd.Scan0;
                                dstp 
+= dstbd.Stride*r;

                                
for (int c = 0; c < dst.Width; ++c)
                                
{
                                    srcp 
= (byte*) srcbd.Scan0;
                                    srcp 
+= srcbd.Stride*(int) (r*m);
                                    srcp 
+= (int) (c*m);

                                    
*dstp++ = *srcp;
                                }

                            }


                        }

                        
finally
                        
{
                            src.UnlockBits(srcbd);
                        }

                    }

                    
finally
                    
{
                        dst.UnlockBits(dstbd);
                    }


                    
return dst;
                }

                
else
                
{
                    
// rely on microsoft guy 

                    
return (Bitmap) src.GetThumbnailImage(width, height, null, IntPtr.Zero);
                }

            }
原文地址:https://www.cnblogs.com/xiaotaoliang/p/284719.html