zoukankan      html  css  js  c++  java
  • 缩略图生成算法

    抄的,避免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);
                    }

                }
  • 相关阅读:
    给<label>点击事件时, 竟然点击了两次
    使用label失效的原因
    vue的又开启
    git使用切换分支等
    【转】 IOS,objective_C中用@interface和 @property 方式声明变量的区别
    【转】iOS-Core-Animation-Advanced-Techniques(六)
    【转】iOS-Core-Animation-Advanced-Techniques(五)
    【转】iOS-Core-Animation-Advanced-Techniques(四)
    【转】iOS-Core-Animation-Advanced-Techniques(三)
    【转】iOS-Core-Animation-Advanced-Techniques(二)
  • 原文地址:https://www.cnblogs.com/xiaotaoliang/p/284719.html
Copyright © 2011-2022 走看看