控制图片尺寸的显示

描述:把图片存储在在SQL Server中,在页面显示时因为图片太大,有时会把网页撑开,影响网页的美观。但又不用缩略图的方式,以下的实现是当图片超过一定尺寸时把限制图片的显示大小,如果小于一定的尺寸就按原尺寸显示。且点击图片后都可在新窗口中浏览。

protected int imgWidth=500;
protected int imgHeight = 430;
Stream stream 
= new MemoryStream();

/// <summary>
/// 处理图片
/// </summary>
/// <param name="imgGuid">图片的GUID</param>

void InitImgFile(string imgGuid)
        
{
            
if (!string.IsNullOrEmpty(imgGuid))
            
{               
                
string _contentType = null;
                
try
                
{
                    DataSet ds 
= Gdf.External.Business.Image.External_CultureGetImageListByGuid(imgGuid);//Gdf.External.Business.Image.External_IntroductionGetImageContent(imgGuid);
                    if (ds != null)
                    
{
                        _contentType 
= ds.Tables[0].Rows[0]["ContentType"].ToString();

                        GetImageSize((
byte[])ds.Tables[0].Rows[0]["Content"]);
                    }

                }

                
catch (Exception ex)
                
{
                    
return;
                }

            }

        }


private void GetImageSize(byte[] buffer)
        
{            
            Bitmap bmp 
= new Bitmap(new MemoryStream(buffer));
            
try
            
{
                System.Drawing.Image.GetThumbnailImageAbort myCallback 
= new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                
//默认显示参数
                int intDefaultWidth = 650;

                
if (intDefaultWidth >= bmp.Width)
                
{
                    
this.imgWidth = bmp.Width;
                    
this.imgHeight = bmp.Height;
                }

                
else
                
{                    
                    
this.imgWidth = 500;
                    
this.imgHeight = 430;

                }

            }

            
catch (Exception ex)
            
{
                
return;

            }

            
finally
            
{
                
// dispose the bmp object
                stream.Position = 0;
                bmp.Dispose();
            }

        }


public bool ThumbnailCallback()
        
{
            
return false;
        }


 
public bool IsReusable
        
{
            
get
            
{
                
return false;
            }

        }
posted @ 2007-06-28 21:42  狐狸马三  阅读(207)  评论(0编辑  收藏  举报