国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
超強(qiáng)C#圖片上傳,加水印,自動(dòng)生成縮略圖源代碼(2)
超強(qiáng)C#圖片上傳,加水印,自動(dòng)生成縮略圖源代碼(2)
 
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
namespace Legalsoft.Images
{
    /// <summary>
    /// News 的摘要說(shuō)明
    /// </summary>
    public class XImage
    {
        public Color tBackground;
        public Color tBorder;
        public Color tShadow;
        public int tQuality;
        public string markPosition;
        /// <summary>
        /// 圖片參數(shù)預(yù)定義
        /// </summary>
        static Hashtable htmimes = new Hashtable();
        internal readonly string AllowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp|.gif";
        public XImage()
        {
            tBackground = Color.Transparent;
            tBorder = Color.Transparent;
            tShadow = Color.Transparent;
            tQuality = 100;
            markPosition = "左下角";
            #region 圖片類型預(yù)定義
            htmimes[".jpe"]="image/jpeg";
            htmimes[".jpeg"] = "image/jpeg";
            htmimes[".jpg"] = "image/jpeg";
            htmimes[".png"] = "image/png";
            htmimes[".tif"] = "image/tiff";
            htmimes[".tiff"] = "image/tiff";
            htmimes[".bmp"] = "image/bmp";
            htmimes[".gif"] = "image/gif";
            #endregion
        }
        #region 下載指定URL圖片并保存
        /// <summary>
        /// 下載指定URL的文件并保存到指定目錄
        /// </summary>
        /// <param name="strUrl"></param>
        public void DownloadImage(string strUrl, string file)
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            wc.DownloadFile(strUrl, file);
        }
        #endregion
        #region C#自動(dòng)生成縮略圖
        /// <summary>
        /// 按給定名字確定顏色
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Color ToColor(string name)
        {
            if (name == "白色") return Color.White;
            if (name == "紅色") return Color.Red;
            if (name == "藍(lán)色") return Color.Blue;
            if (name == "綠色") return Color.Green;
            if (name == "黑色") return Color.Black;
            if (name == "灰色") return Color.DarkGray;
            if (name == "黃色") return Color.Yellow;
            if (name == "紫色") return Color.Cyan;
            if (name == "無(wú)色") return Color.Transparent;
            return Color.Transparent;
        }
        /// <summary>
        /// 按名字設(shè)置各種顏色,可以自行擴(kuò)充:)
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int ToQuality(string name)
        {
            return Int32.Parse(name.Replace("%", ""));
        }
        /// <summary>
        /// 獲取圖像編碼解碼器的所有相關(guān)信息
        /// </summary>
        /// <param name="mimeType">包含編碼解碼器的多用途網(wǎng)際郵件擴(kuò)充協(xié)議 (MIME) 類型的字符串</param>
        /// <returns>返回圖像編碼解碼器的所有相關(guān)信息</returns>
        private static ImageCodecInfo GetCodecInfo(string mimeType)
        {
            ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo ici in CodecInfo)
            {
                if (ici.MimeType == mimeType) return ici;
            }
            return null;
        }
        /// <summary>
        /// 檢測(cè)擴(kuò)展名的有效性
        /// </summary>
        /// <param name="sExt">文件名擴(kuò)展名</param>
        /// <returns>如果擴(kuò)展名有效,返回true,否則返回false.</returns>
        private bool CheckValidExt(string sExt)
        {
            bool flag = false;
            string[] aExt = AllowExt.Split("|");
            foreach (string filetype in aExt)
            {
                if (filetype.ToLower() == sExt)
                {
                    flag = true;
                    break;
                }
            }
            return flag;
        }
        /// <summary>
        /// 保存圖片
        /// </summary>
        /// <param name="image">Image 對(duì)象</param>
        /// <param name="savePath">保存路徑</param>
        /// <param name="ici">指定格式的編解碼參數(shù)</param>
        private void SaveImage(System.Drawing.Image image, string savePath, ImageCodecInfo ici)
        {
            //設(shè)置 原圖片 對(duì)象的 EncoderParameters 對(duì)象
            EncoderParameters parameters = new EncoderParameters(1);
            parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ((long)tQuality));
            image.Save(savePath, ici, parameters);
            parameters.Dispose();
        }
        /// <summary>
        /// 生成縮略圖
        /// </summary>
        /// <param name="sourceImagePath">原圖片路徑(相對(duì)路徑)</param>
        /// <param name="thumbnailImagePath">生成的縮略圖路徑,如果為空則保存為原圖片路徑(相對(duì)路徑)</param>
        /// <param name="thumbnailImageWidth">縮略圖的寬度(高度與按源圖片比例自動(dòng)生成)</param>
        public void ToThumbnail(string sourceImagePath, string thumbnailImagePath, int thumbnailImageWidth, int thumbnailImageHeight )
        {
            // 1.先檢查圖片格式等信息
            string ThumbnailImagePath = thumbnailImagePath;
            string SourceImagePath = sourceImagePath;
            string sExt = SourceImagePath.Substring(SourceImagePath.LastIndexOf(".")).ToLower();
            if (SourceImagePath.ToString() == System.String.Empty)
            {
                throw new NullReferenceException("SourceImagePath is null!");
            }
            if (!CheckValidExt(sExt))
            {
                throw new ArgumentException("原圖片文件格式不正確,支持的格式有[ " + AllowExt + " ]", "SourceImagePath");
            }
            // 從原圖片創(chuàng)建 Image 對(duì)象
            System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(SourceImagePath));
            // 2.計(jì)算圖片的位置、尺寸等信息
            int tWidth, tHeight, tLeft, tTop;
            double fScale = (double)thumbnailImageHeight / (double)thumbnailImageWidth; // 高度寬度比
            if (((double)image.Width * fScale) > (double)image.Height) // 如果原圖比較寬
            {
                tWidth = thumbnailImageWidth;
                tHeight = (int)((double)image.Height * (double)tWidth / (double)image.Width);
                tLeft = 0;
                tTop = (thumbnailImageHeight-tHeight)/2;
            }
            else
            {
                tHeight = thumbnailImageHeight;
                tWidth = (int)((double)image.Width * (double)tHeight / (double)image.Height);
                tLeft = (thumbnailImageWidth-tWidth)/2;
                tTop = 0;
            }
            if (tLeft < 0) tLeft = 0;
            if (tTop < 0) tTop = 0;
            if (tBorder != Color.Transparent)
            {
                tWidth -= 2;
                tHeight -= 2;
                tLeft++;
                tTop++;
            }
            if (tShadow != Color.Transparent)
            {
                tWidth -= 1;
                tHeight -= 1;
            }
            //用指定的大小和格式初始化 Bitmap 類的新實(shí)例
            //Bitmap bitmap = new Bitmap(ThumbnailImageWidth, num, PixelFormat.Format32bppArgb);
            Bitmap bitmap = new Bitmap(thumbnailImageWidth, thumbnailImageHeight, PixelFormat.Format32bppArgb);
            //從指定的 Image 對(duì)象創(chuàng)建新 Graphics 對(duì)象
            Graphics graphics = Graphics.FromImage(bitmap);
            //清除整個(gè)繪圖面并以透明背景色填充
            if (tBackground != Color.Transparent)
            {
                graphics.Clear(tBackground);
            }
            else
            {
                graphics.Clear(Color.Transparent);
            }
            // 添加陰影
            if (tShadow != Color.Transparent)
            {
                Pen shPen = new Pen(tShadow);
                graphics.DrawLine(shPen, new Point(1, thumbnailImageHeight-1), new Point(thumbnailImageWidth-1, thumbnailImageHeight-1));
                graphics.DrawLine(shPen, new Point(thumbnailImageWidth-1, 1), new Point(thumbnailImageWidth-1, thumbnailImageHeight-1));
            }
            // 添加邊框
            if (tBorder != Color.Transparent)
            {
                Pen bdPen = new Pen(tBorder);
                if (tShadow != Color.Transparent)
                {
                    graphics.DrawRectangle(bdPen, new Rectangle(0, 0, thumbnailImageWidth-2, thumbnailImageHeight-2));
                }
                else
                {
                    graphics.DrawRectangle(bdPen, new Rectangle(0, 0, thumbnailImageWidth-1, thumbnailImageHeight-1));
                }
            }
            //在指定位置并且按指定大小繪制 原圖片 對(duì)象
            graphics.DrawImage(image, new Rectangle(tLeft, tTop, tWidth, tHeight));
            image.Dispose();
            try
            {
                //將此 原圖片 以指定格式并用指定的編解碼參數(shù)保存到指定文件
                string savepath = (ThumbnailImagePath == null ? SourceImagePath : ThumbnailImagePath);
                SaveImage(bitmap, HttpContext.Current.Server.MapPath(savepath), GetCodecInfo((string)htmimes[sExt]));
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                bitmap.Dispose();
                graphics.Dispose();
            }
        }
        #endregion
        #region C#給圖片添加水印
        public void Mark(string sourceImagePath, string markString)
        {
            // 1.先檢查圖片格式等信息
            string markImagePath = sourceImagePath;
            string SourceImagePath = sourceImagePath;
            string sExt = SourceImagePath.Substring(SourceImagePath.LastIndexOf(".")).ToLower();
            if (SourceImagePath.ToString() == System.String.Empty)
            {
                throw new NullReferenceException("SourceImagePath is null!");
            }
            if (!CheckValidExt(sExt))
            {
                throw new ArgumentException("原圖片文件格式不正確,支持的格式有[ " + AllowExt + " ]", "SourceImagePath");
            }
            // 從原圖片創(chuàng)建 Image 對(duì)象
            System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(SourceImagePath));
            //用指定的大小和格式初始化 Bitmap 類的新實(shí)例
            Bitmap bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
            //從指定的 Image 對(duì)象創(chuàng)建新 Graphics 對(duì)象
            Graphics graphics = Graphics.FromImage(bitmap);
            //在指定位置并且按指定大小繪制 原圖片 對(duì)象
            graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height));
            #region 繪制水印
            // 設(shè)置水印字體
            int fHeight = image.Height/5;
            if(fHeight>16) fHeight = 16;
            Font drawFont = new Font("Arial", fHeight);
            // 設(shè)置水印文字位置,默認(rèn)為左下角
            float x = 4;
            float y = image.Height - drawFont.Height - 4;
            if (markPosition == "左上角")
            {
                y = 4;
            }
            if (markPosition == "右上角")
            {
                x = image.Width - markString.Length * fHeight / 2 - fHeight;
                y = 4;
            }
            if (markPosition == "右下角")
            {
                x = image.Width - markString.Length * fHeight / 2 - fHeight;
            }
            if (markPosition == "圖片中間")
            {
                x = image.Width / 2 - markString.Length * fHeight / 2;
                y = image.Height / 2 - fHeight / 2;
            }
            StringFormat drawFormat = new StringFormat();
            drawFormat.FormatFlags = StringFormatFlags.NoWrap;
            // 設(shè)置水印文字顏色,先繪制一個(gè)黑色字作為陰影,再繪制白色字,這樣比較顯眼;
            SolidBrush drawBrush = new SolidBrush(Color.Black);
            graphics.DrawString(markString, drawFont, drawBrush, x, y, drawFormat);
            drawBrush.Color = Color.White;
            graphics.DrawString(markString, drawFont, drawBrush, x-1, y-1, drawFormat);
            #endregion
            image.Dispose();
            try
            {
                //將此 原圖片 以指定格式并用指定的編解碼參數(shù)保存到指定文件
                string savepath = SourceImagePath;
                SaveImage(bitmap, HttpContext.Current.Server.MapPath(savepath), GetCodecInfo((string)htmimes[sExt]));
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                bitmap.Dispose();
                graphics.Dispose();
            }
        }
        #endregion
    }
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C#圖片處理高級(jí)應(yīng)用(裁剪,縮放,清晰度,水印) - 吳劍-WEB應(yīng)用 - 博客園
DataGridView控件中顯示圖片及其注意事項(xiàng)
關(guān)于asp.net上傳圖片自動(dòng)生成縮略圖
c#截圖工具
生成驗(yàn)證碼
如何在asp.net中如何在線播放視頻文件
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服