控制图片尺寸的显示

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

None.gif protected   int  imgWidth = 500 ;
None.gif
protected   int  imgHeight  =   430 ;
None.gifStream stream 
=   new  MemoryStream();
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif
/// 处理图片
InBlock.gif
/// </summary>
ExpandedBlockEnd.gif
/// <param name="imgGuid">图片的GUID</param>

None.gif void  InitImgFile( string  imgGuid)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if (!string.IsNullOrEmpty(imgGuid))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{               
InBlock.gif                
string _contentType = null;
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DataSet ds 
= Gdf.External.Business.Image.External_CultureGetImageListByGuid(imgGuid);//Gdf.External.Business.Image.External_IntroductionGetImageContent(imgGuid);
InBlock.gif
                    if (ds != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        _contentType 
= ds.Tables[0].Rows[0]["ContentType"].ToString();
InBlock.gif
InBlock.gif                        GetImageSize((
byte[])ds.Tables[0].Rows[0]["Content"]);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif
private   void  GetImageSize( byte [] buffer)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {            
InBlock.gif            Bitmap bmp 
= new Bitmap(new MemoryStream(buffer));
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Drawing.Image.GetThumbnailImageAbort myCallback 
= new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
InBlock.gif                
//默认显示参数
InBlock.gif
                int intDefaultWidth = 650;
InBlock.gif
InBlock.gif                
if (intDefaultWidth >= bmp.Width)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.imgWidth = bmp.Width;
InBlock.gif                    
this.imgHeight = bmp.Height;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    
this.imgWidth = 500;
InBlock.gif                    
this.imgHeight = 430;
InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// dispose the bmp object
InBlock.gif
                stream.Position = 0;
InBlock.gif                bmp.Dispose();
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif
public   bool  ThumbnailCallback()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
return false;
ExpandedBlockEnd.gif        }

None.gif
None.gif 
public   bool  IsReusable
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/foxhorse/archive/2007/06/28/799389.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值