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

                }
  • 相关阅读:
    linux查看内存占用情况
    tomcat JVM内存 配置
    Linux中iptables设置详细
    asmack xmpp 获取离线消息
    linux查看端口被哪个服务占用的命令
    Redis 3.0版本启动时出现警告的解决办法
    redis中密码设置
    linux安装(Ubuntu)——(二)
    Linux简介——(一)
    Android控件——ToggleButton多状态按钮(实现灯泡的开关)
  • 原文地址:https://www.cnblogs.com/xiaotaoliang/p/284719.html
Copyright © 2011-2022 走看看