首页  ·  知识 ·  编程语言
生成高质量缩略图
网友  收集  .NET  编辑:德仔   图片来源:网络
color: #008000/// color: #008000方法1/
//方法1
public static Bitmap CreateThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect)
        {
            
// return the source image if it's smaller than the designated thumbnail
            if (source.Width < thumbWi && source.Height < thumbHi) return source;

            System.Drawing.Bitmap ret 
= null;
            
try
            {
                
int wi, hi;

                wi 
= thumbWi;
                hi 
= thumbHi;

                
if (maintainAspect)
                {
                    
// maintain the aspect ratio despite the thumbnail size parameters
                    if (source.Width > source.Height)
                    {
                        wi 
= thumbWi;
                        hi 
= (int)(source.Height * ((decimal)thumbWi / source.Width));
                    }
                    
else
                    {
                        hi 
= thumbHi;
                        wi 
= (int)(source.Width * ((decimal)thumbHi / source.Height));
                    }
                }

                
// original code that creates lousy thumbnails
                
// System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);
                ret = new Bitmap(wi, hi);
                
using (Graphics g = Graphics.FromImage(ret))
                {
                    g.InterpolationMode 
= System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.FillRectangle(Brushes.White, 
00, wi, hi);
                    g.DrawImage(source, 
00, wi, hi);
                }
            }
            
catch
            {
                ret 
= null;
            }

            
return ret;
        }
//调用
 using (Graphics g = Graphics.FromImage(ret))
                {
                    g.InterpolationMode 
= System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.FillRectangle(Brushes.White, 
00, wi, hi);
                    g.DrawImage(source, 
00, wi, hi);
                }

将文件保存到数据库中
 

代码
//保存文件到SQL Server数据库中
    private void FileToSql(string fileName,string tableName,string fieldName)
    {
        SqlConnection cn
=new SqlConnection ();
        FileInfo fi
=new FileInfo(fileName);
        FileStream fs
=fi.OpenRead();
        
byte[] bytes=new byte[fs.Length];
        fs.Read(bytes,
0,Convert.ToInt32(fs.Length));
        SqlCommand cm
=new SqlCommand();
        cm.Connection
=cn;
        cm.CommandType
=CommandType.Text;
        
if(cn.State==0) cn.Open();
        cm.CommandText
="insert into "+tableName+"("+fieldName+") values(@file)";
        SqlParameter spFile
=new SqlParameter("@file",SqlDbType.Image);
        spFile.Value
=bytes;
        cm.Parameters.Add(spFile);
        cm.ExecuteNonQuery();
    }
//保存文件到Access数据库中
    private void FileToAccess(string fileName,string tableName,string fieldName)
    {
        OleDbConnection cn
=new OleDbConnection ();
        FileInfo fi
=new FileInfo(fileName);
        FileStream fs
=fi.OpenRead();
        
byte[] bytes=new byte[fs.Length];
        fs.Read(bytes,
0,Convert.ToInt32(fs.Length));
        OleDbCommand cm
=new OleDbCommand();
        cm.Connection
=cn;
        cm.CommandType
=CommandType.Text;
        
if(cn.State==0) cn.Open();
        cm.CommandText
="insert into "+tableName+"("+fieldName+") values(@file)";
        OleDbParameter spFile
=new OleDbParameter("@file",OleDbType.Binary);
        spFile.Value
=bytes;
        cm.Parameters.Add(spFile);
        cm.ExecuteNonQuery();
    }
    
//保存客户端文件到数据库,fl_name为上传控件
    private void FileToDataSourse(string mailid)
    {
        
string ConnStr = "";
        
string sql = "update t_mail set attachfilename=@attachfilename,attachfile=@attachfile where mailid=" + mailid;
        SqlCommand myCommand 
= new SqlCommand(sql, new SqlConnection(ConnStr));
        
string path = fl_name.PostedFile.FileName;
        
string filename = path.Substring(path.LastIndexOf(""+ 1, path.Length - path.LastIndexOf(""- 1);
        myCommand.Parameters.Add(
"@attachfilename", SqlDbType.VarChar);
        myCommand.Parameters[
"@attachfilename"].Value = filename;
        myCommand.Parameters.Add(
"@attachfile", SqlDbType.Image);
        Stream fileStream 
= fl_name.PostedFile.InputStream;
        
int intFileSize = fl_name.PostedFile.ContentLength;
        
byte[] fileContent = new byte[intFileSize];
        
int intStatus = fileStream.Read(fileContent, 0, intFileSize); //文件读取到fileContent数组中
        myCommand.Parameters["@attachfile"].Value = ((byte[])fileContent);
        fileStream.Close();
        myCommand.Connection.Open();
        myCommand.ExecuteNonQuery();
        myCommand.Connection.Close();
    }

将用户输入的字符串转换为可换行、替换Html编码、无危害数据库特殊字符、去掉首尾空白、的安全方便代码
 

代码
public static string ConvertStr(string inputString)
        {
            
string retVal = inputString;
            retVal 
= retVal.Replace("&""&amp;");
            retVal 
= retVal.Replace("\"""&quot;");
            retVal = retVal.Replace("<""&lt;");
            retVal 
= retVal.Replace(">""&gt;");
            retVal 
= retVal.Replace(" ""&nbsp;");
            retVal 
= retVal.Replace("  ""&nbsp;&nbsp;");
            retVal 
= retVal.Replace("\t""&nbsp;&nbsp;");
            retVal 
= retVal.Replace("\r""<br>");
            
return retVal;
        }

        
private static string FetchURL(string strMessage)
        {
            
string strPattern = @"(?<url>(http|ftp|mms|rstp|news|https)://(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&~=]*[^.\s|,|\)|<|!])?)";
            
string strReplace = "<a href=\"${url}\" target=_blank>${url}</a>";
            
string strInput = strMessage;
            
string strResult;
            strResult 
= Regex.Replace(strInput, strPattern, strReplace);
            strPattern 
= @"(?<!http://)(?<url>www\.(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&~=]*[^.\s|,|\)|<|!])?)";
            strReplace 
= "<a href=\"http://${url}\" target=_blank>${url}</a>";
            strResult = Regex.Replace(strResult, strPattern, strReplace);
            
return strResult;
        } 

        
public string ToUrl(string inputString)
        {
            
string retVal = inputString;
            retVal 
= ConvertStr(retVal);
            retVal 
= FetchURL(retVal);
            
return retVal;
        }
本文作者:网友 来源:网络收集
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读