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);
                    }

                }
  • 相关阅读:
    python报错Enable tracemalloc to get the object allocation traceback
    解决pycharm每次新建项目都要重新安装一些第三方库的问题
    创建一个CA证书
    [转载]oracle 12C 《服务器、客户端安装》
    [转载]Windows Server 2016中添加AD域控制器
    [转载]Windows Server 2016中部署AD
    虚拟机 VMware Workstation Pro 15.5.0 及永久激活密钥
    Oracle给查询结果增加序列号
    创建自定义ssl证书用于https
    js:getAttribute
  • 原文地址:https://www.cnblogs.com/xiaotaoliang/p/284719.html
Copyright © 2011-2022 走看看