魔兽争霸3冰封王座地图下载

 找回密碼
 馬上注冊

QQ登錄

只需一步,快速開始

掃一掃,訪問微社區

分布式系統框架(V1.3) 輕松承載百億數據,千萬流量!討論專區 - 源碼下載 - 官方教程

HttpHelper萬能框架(V2.3-含.netcore) HttpHelper官方出品,無敵框架討論區 - 源碼下載 - 在線測試和代碼生成

HttpHelper爬蟲類(V2.0) 最牛的爬蟲類,沒有爬不到只有想不到 源碼 - 代碼生成器 - 討論區 - 教程- 例子

查看: 26306|回復: 56
打印 上一主題 下一主題

魔兽争霸之冰封王座咋下载电脑上: [GDI+] C# ImageClass幫助類教程與源碼下載

[復制鏈接]
跳轉到指定樓層
樓主
發表于 2013-3-15 11:05:58 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
               C# ImageClass幫助類教程與源碼下載
導讀部分
-------------------------------------------------------------------------------------------------------------
C#基類|C#自定義類|C#幫助類--系列導航文章
//www.6251207.com/thread-655-1-1.html

下載附件需要回復哦
ImageClass.rar (4.4 KB, 下載次數: 1621)
功能如下圖片



主要功能有:
縮略圖片,圖片水印,文字水印,調整光暗,反色處理,浮雕處理,拉伸處理,
左右翻轉,上下翻轉,壓縮圖片,圖片灰度化,轉換為黑白圖片,獲取圖片中的各幀
看代碼吧
[C#] 純文本查看 復制代碼
/// <summary>
/// 類說明:Assistant
/// 編 碼 人:蘇飛
/// 聯系方式:361983679  
/// 更新網站:[url=//www.6251207.com/thread-655-1-1.html]//www.6251207.com/thread-655-1-1.html[/url]
/// </summary>
using System;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace DotNet.Utilities
{
    public class ImageClass
    {
        public ImageClass()
        { }

        #region 縮略圖
        /// <summary>
        /// 生成縮略圖
        /// </summary>
        /// <param name="originalImagePath">源圖路徑(物理路徑)</param>
        /// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param>
        /// <param name="width">縮略圖寬度</param>
        /// <param name="height">縮略圖高度</param>
        /// <param name="mode">生成縮略圖的方式</param>    
        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

            int towidth = width;
            int toheight = height;

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            switch (mode)
            {
                case "HW":  //指定高寬縮放(可能變形)                
                    break;
                case "W":   //指定寬,高按比例                    
                    toheight = originalImage.Height * width / originalImage.Width;
                    break;
                case "H":   //指定高,寬按比例
                    towidth = originalImage.Width * height / originalImage.Height;
                    break;
                case "Cut": //指定高寬裁減(不變形)                
                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                    {
                        oh = originalImage.Height;
                        ow = originalImage.Height * towidth / toheight;
                        y = 0;
                        x = (originalImage.Width - ow) / 2;
                    }
                    else
                    {
                        ow = originalImage.Width;
                        oh = originalImage.Width * height / towidth;
                        x = 0;
                        y = (originalImage.Height - oh) / 2;
                    }
                    break;
                default:
                    break;
            }

            //新建一個bmp圖片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

            //新建一個畫板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            //設置高質量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //設置高質量,低速度呈現平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空畫布并以透明背景色填充
            g.Clear(System.Drawing.Color.Transparent);

            //在指定位置并且按指定大小繪制原圖片的指定部分
            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);

            try
            {
                //以jpg格式保存縮略圖
                bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }
        #endregion

        #region 圖片水印
        /// <summary>
        /// 圖片水印處理方法
        /// </summary>
        /// <param name="path">需要加載水印的圖片路徑(絕對路徑)</param>
        /// <param name="waterpath">水印圖片(絕對路徑)</param>
        /// <param name="location">水印位置(傳送正確的代碼)</param>
        public static string ImageWatermark(string path, string waterpath, string location)
        {
            string kz_name = Path.GetExtension(path);
            if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
            {
                DateTime time = DateTime.Now;
                string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
                Image img = Bitmap.FromFile(path);
                Image waterimg = Image.FromFile(waterpath);
                Graphics g = Graphics.FromImage(img);
                ArrayList loca = GetLocation(location, img, waterimg);
                g.DrawImage(waterimg, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), waterimg.Width, waterimg.Height));
                waterimg.Dispose();
                g.Dispose();
                string newpath = Path.GetDirectoryName(path) + filename + kz_name;
                img.Save(newpath);
                img.Dispose();
                File.Copy(newpath, path, true);
                if (File.Exists(newpath))
                {
                    File.Delete(newpath);
                }
            }
            return path;
        }

        /// <summary>
        /// 圖片水印位置處理方法
        /// </summary>
        /// <param name="location">水印位置</param>
        /// <param name="img">需要添加水印的圖片</param>
        /// <param name="waterimg">水印圖片</param>
        private static ArrayList GetLocation(string location, Image img, Image waterimg)
        {
            ArrayList loca = new ArrayList();
            int x = 0;
            int y = 0;

            if (location == "LT")
            {
                x = 10;
                y = 10;
            }
            else if (location == "T")
            {
                x = img.Width / 2 - waterimg.Width / 2;
                y = img.Height - waterimg.Height;
            }
            else if (location == "RT")
            {
                x = img.Width - waterimg.Width;
                y = 10;
            }
            else if (location == "LC")
            {
                x = 10;
                y = img.Height / 2 - waterimg.Height / 2;
            }
            else if (location == "C")
            {
                x = img.Width / 2 - waterimg.Width / 2;
                y = img.Height / 2 - waterimg.Height / 2;
            }
            else if (location == "RC")
            {
                x = img.Width - waterimg.Width;
                y = img.Height / 2 - waterimg.Height / 2;
            }
            else if (location == "LB")
            {
                x = 10;
                y = img.Height - waterimg.Height;
            }
            else if (location == "B")
            {
                x = img.Width / 2 - waterimg.Width / 2;
                y = img.Height - waterimg.Height;
            }
            else
            {
                x = img.Width - waterimg.Width;
                y = img.Height - waterimg.Height;
            }
            loca.Add(x);
            loca.Add(y);
            return loca;
        }
        #endregion

        #region 文字水印
        /// <summary>
        /// 文字水印處理方法
        /// </summary>
        /// <param name="path">圖片路徑(絕對路徑)</param>
        /// <param name="size">字體大小</param>
        /// <param name="letter">水印文字</param>
        /// <param name="color">顏色</param>
        /// <param name="location">水印位置</param>
        public static string LetterWatermark(string path, int size, string letter, Color color, string location)
        {
            #region

            string kz_name = Path.GetExtension(path);
            if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
            {
                DateTime time = DateTime.Now;
                string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
                Image img = Bitmap.FromFile(path);
                Graphics gs = Graphics.FromImage(img);
                ArrayList loca = GetLocation(location, img, size, letter.Length);
                Font font = new Font("宋體", size);
                Brush br = new SolidBrush(color);
                gs.DrawString(letter, font, br, float.Parse(loca[0].ToString()), float.Parse(loca[1].ToString()));
                gs.Dispose();
                string newpath = Path.GetDirectoryName(path) + filename + kz_name;
                img.Save(newpath);
                img.Dispose();
                File.Copy(newpath, path, true);
                if (File.Exists(newpath))
                {
                    File.Delete(newpath);
                }
            }
            return path;

            #endregion
        }

        /// <summary>
        /// 文字水印位置的方法
        /// </summary>
        /// <param name="location">位置代碼</param>
        /// <param name="img">圖片對象</param>
        /// <param name="width">寬(當水印類型為文字時,傳過來的就是字體的大小)</param>
        /// <param name="height">高(當水印類型為文字時,傳過來的就是字符的長度)</param>
        private static ArrayList GetLocation(string location, Image img, int width, int height)
        {
            #region

            ArrayList loca = new ArrayList();  //定義數組存儲位置
            float x = 10;
            float y = 10;

            if (location == "LT")
            {
                loca.Add(x);
                loca.Add(y);
            }
            else if (location == "T")
            {
                x = img.Width / 2 - (width * height) / 2;
                loca.Add(x);
                loca.Add(y);
            }
            else if (location == "RT")
            {
                x = img.Width - width * height;
            }
            else if (location == "LC")
            {
                y = img.Height / 2;
            }
            else if (location == "C")
            {
                x = img.Width / 2 - (width * height) / 2;
                y = img.Height / 2;
            }
            else if (location == "RC")
            {
                x = img.Width - height;
                y = img.Height / 2;
            }
            else if (location == "LB")
            {
                y = img.Height - width - 5;
            }
            else if (location == "B")
            {
                x = img.Width / 2 - (width * height) / 2;
                y = img.Height - width - 5;
            }
            else
            {
                x = img.Width - width * height;
                y = img.Height - width - 5;
            }
            loca.Add(x);
            loca.Add(y);
            return loca;

            #endregion
        }
        #endregion

        #region 調整光暗
        /// <summary>
        /// 調整光暗
        /// </summary>
        /// <param name="mybm">原始圖片</param>
        /// <param name="width">原始圖片的長度</param>
        /// <param name="height">原始圖片的高度</param>
        /// <param name="val">增加或減少的光暗值</param>
        public Bitmap LDPic(Bitmap mybm, int width, int height, int val)
        {
            Bitmap bm = new Bitmap(width, height);//初始化一個記錄經過處理后的圖片對象
            int x, y, resultR, resultG, resultB;//x、y是循環次數,后面三個是記錄紅綠藍三個值的
            Color pixel;
            for (x = 0; x < width; x++)
            {
                for (y = 0; y < height; y++)
                {
                    pixel = mybm.GetPixel(x, y);//獲取當前像素的值
                    resultR = pixel.R + val;//檢查紅色值會不會超出[0, 255]
                    resultG = pixel.G + val;//檢查綠色值會不會超出[0, 255]
                    resultB = pixel.B + val;//檢查藍色值會不會超出[0, 255]
                    bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//繪圖
                }
            }
            return bm;
        }
        #endregion

        #region 反色處理
        /// <summary>
        /// 反色處理
        /// </summary>
        /// <param name="mybm">原始圖片</param>
        /// <param name="width">原始圖片的長度</param>
        /// <param name="height">原始圖片的高度</param>
        public Bitmap RePic(Bitmap mybm, int width, int height)
        {
            Bitmap bm = new Bitmap(width, height);//初始化一個記錄處理后的圖片的對象
            int x, y, resultR, resultG, resultB;
            Color pixel;
            for (x = 0; x < width; x++)
            {
                for (y = 0; y < height; y++)
                {
                    pixel = mybm.GetPixel(x, y);//獲取當前坐標的像素值
                    resultR = 255 - pixel.R;//反紅
                    resultG = 255 - pixel.G;//反綠
                    resultB = 255 - pixel.B;//反藍
                    bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//繪圖
                }
            }
            return bm;
        }
        #endregion

        #region 浮雕處理
        /// <summary>
        /// 浮雕處理
        /// </summary>
        /// <param name="oldBitmap">原始圖片</param>
        /// <param name="Width">原始圖片的長度</param>
        /// <param name="Height">原始圖片的高度</param>
        public Bitmap FD(Bitmap oldBitmap, int Width, int Height)
        {
            Bitmap newBitmap = new Bitmap(Width, Height);
            Color color1, color2;
            for (int x = 0; x < Width - 1; x++)
            {
                for (int y = 0; y < Height - 1; y++)
                {
                    int r = 0, g = 0, b = 0;
                    color1 = oldBitmap.GetPixel(x, y);
                    color2 = oldBitmap.GetPixel(x + 1, y + 1);
                    r = Math.Abs(color1.R - color2.R + 128);
                    g = Math.Abs(color1.G - color2.G + 128);
                    b = Math.Abs(color1.B - color2.B + 128);
                    if (r > 255) r = 255;
                    if (r < 0) r = 0;
                    if (g > 255) g = 255;
                    if (g < 0) g = 0;
                    if (b > 255) b = 255;
                    if (b < 0) b = 0;
                    newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                }
            }
            return newBitmap;
        }
        #endregion

        #region 拉伸圖片
        /// <summary>
        /// 拉伸圖片
        /// </summary>
        /// <param name="bmp">原始圖片</param>
        /// <param name="newW">新的寬度</param>
        /// <param name="newH">新的高度</param>
        public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
        {
            try
            {
                Bitmap bap = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(bap);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(bap, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bap.Width, bap.Height), GraphicsUnit.Pixel);
                g.Dispose();
                return bap;
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region 濾色處理
        /// <summary>
        /// 濾色處理
        /// </summary>
        /// <param name="mybm">原始圖片</param>
        /// <param name="width">原始圖片的長度</param>
        /// <param name="height">原始圖片的高度</param>
        public Bitmap FilPic(Bitmap mybm, int width, int height)
        {
            Bitmap bm = new Bitmap(width, height);//初始化一個記錄濾色效果的圖片對象
            int x, y;
            Color pixel;

            for (x = 0; x < width; x++)
            {
                for (y = 0; y < height; y++)
                {
                    pixel = mybm.GetPixel(x, y);//獲取當前坐標的像素值
                    bm.SetPixel(x, y, Color.FromArgb(0, pixel.G, pixel.B));//繪圖
                }
            }
            return bm;
        }
        #endregion

        #region 左右翻轉
        /// <summary>
        /// 左右翻轉
        /// </summary>
        /// <param name="mybm">原始圖片</param>
        /// <param name="width">原始圖片的長度</param>
        /// <param name="height">原始圖片的高度</param>
        public Bitmap RevPicLR(Bitmap mybm, int width, int height)
        {
            Bitmap bm = new Bitmap(width, height);
            int x, y, z; //x,y是循環次數,z是用來記錄像素點的x坐標的變化的
            Color pixel;
            for (y = height - 1; y >= 0; y--)
            {
                for (x = width - 1, z = 0; x >= 0; x--)
                {
                    pixel = mybm.GetPixel(x, y);//獲取當前像素的值
                    bm.SetPixel(z++, y, Color.FromArgb(pixel.R, pixel.G, pixel.B));//繪圖
                }
            }
            return bm;
        }
        #endregion

        #region 上下翻轉
        /// <summary>
        /// 上下翻轉
        /// </summary>
        /// <param name="mybm">原始圖片</param>
        /// <param name="width">原始圖片的長度</param>
        /// <param name="height">原始圖片的高度</param>
        public Bitmap RevPicUD(Bitmap mybm, int width, int height)
        {
            Bitmap bm = new Bitmap(width, height);
            int x, y, z;
            Color pixel;
            for (x = 0; x < width; x++)
            {
                for (y = height - 1, z = 0; y >= 0; y--)
                {
                    pixel = mybm.GetPixel(x, y);//獲取當前像素的值
                    bm.SetPixel(x, z++, Color.FromArgb(pixel.R, pixel.G, pixel.B));//繪圖
                }
            }
            return bm;
        }
        #endregion

        #region 壓縮圖片
        /// <summary>
        /// 壓縮到指定尺寸
        /// </summary>
        /// <param name="oldfile">原文件</param>
        /// <param name="newfile">新文件</param>
        public bool Compress(string oldfile, string newfile)
        {
            return Compress(oldfile, newfile, 100, 125);
        }
        /// <summary>
        /// 壓縮指定尺寸,如果寫的和圖片大家一樣表示大小不變,只是把圖片壓縮下一些
        /// </summary>
        /// <param name="oldfile">原文件</param>
        /// <param name="newfile">新文件</param>
        /// <param name="width">長</param>
        /// <param name="height">高</param>
        public bool Compress(string oldfile, string newfile, int width, int height)
        {
            try
            {
                System.Drawing.Image img = System.Drawing.Image.FromFile(oldfile);
                System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
                Size newSize = new Size(width, height);
                Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
                Graphics g = Graphics.FromImage(outBmp);
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
                g.Dispose();
                EncoderParameters encoderParams = new EncoderParameters();
                long[] quality = new long[1];
                quality[0] = 100;
                EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                encoderParams.Param[0] = encoderParam;
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICI = null;
                for (int x = 0; x < arrayICI.Length; x++)
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICI = arrayICI[x]; //設置JPEG編碼
                        break;
                    }
                img.Dispose();
                if (jpegICI != null) outBmp.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
                outBmp.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region 圖片灰度化
        public Color Gray(Color c)
        {
            int rgb = Convert.ToInt32((double)(((0.3 * c.R) + (0.59 * c.G)) + (0.11 * c.B)));
            return Color.FromArgb(rgb, rgb, rgb);
        }
        #endregion

        #region 轉換為黑白圖片
        /// <summary>
        /// 轉換為黑白圖片
        /// </summary>
        /// <param name="mybt">要進行處理的圖片</param>
        /// <param name="width">圖片的長度</param>
        /// <param name="height">圖片的高度</param>
        public Bitmap BWPic(Bitmap mybm, int width, int height)
        {
            Bitmap bm = new Bitmap(width, height);
            int x, y, result; //x,y是循環次數,result是記錄處理后的像素值
            Color pixel;
            for (x = 0; x < width; x++)
            {
                for (y = 0; y < height; y++)
                {
                    pixel = mybm.GetPixel(x, y);//獲取當前坐標的像素值
                    result = (pixel.R + pixel.G + pixel.B) / 3;//取紅綠藍三色的平均值
                    bm.SetPixel(x, y, Color.FromArgb(result, result, result));
                }
            }
            return bm;
        }
        #endregion

        #region 獲取圖片中的各幀
        /// <summary>
        /// 獲取圖片中的各幀
        /// </summary>
        /// <param name="pPath">圖片路徑</param>
        /// <param name="pSavePath">保存路徑</param>
        public void GetFrames(string pPath, string pSavedPath)
        {
            Image gif = Image.FromFile(pPath);
            FrameDimension fd = new FrameDimension(gif.FrameDimensionsList[0]);
            int count = gif.GetFrameCount(fd); //獲取幀數(gif圖片可能包含多幀,其它格式圖片一般僅一幀)
            for (int i = 0; i < count; i++)    //以Jpeg格式保存各幀
            {
                gif.SelectActiveFrame(fd, i);
                gif.Save(pSavedPath + "\\frame_" + i + ".jpg", ImageFormat.Jpeg);
            }
        }
        #endregion
    }
}

推薦
發表于 2013-3-15 11:39:04 | 只看該作者
板凳
發表于 2013-3-15 19:20:40 | 只看該作者
地板
發表于 2013-3-16 11:50:23 | 只看該作者
5
發表于 2013-3-28 21:48:41 | 只看該作者
6
發表于 2013-4-2 15:20:55 | 只看該作者
飛哥你太猛了  收集的真全,以前我也整理過但是沒你的全  嘿嘿牛逼
7
發表于 2013-4-2 15:22:02 | 只看該作者
8
 樓主| 發表于 2013-4-2 15:39:08 | 只看該作者
7685780 發表于 2013-4-2 15:22
真當心會被你慣壞了  有你在就不想去整理類了

我會一直堅持開源的。你們可以學的勤快點,看看源碼。
9
發表于 2013-5-4 17:46:25 | 只看該作者
10
發表于 2013-5-23 12:34:02 | 只看該作者
您需要登錄后才可以回帖 登錄 | 馬上注冊

本版積分規則

QQ|手機版|小黑屋|手機版|聯系我們|關于我們|廣告合作|魔兽争霸3冰封王座地图下载 ( 豫ICP備17001017號-1)

GMT+8, 2019-10-19 04:45

© 2017-2018

快速回復 魔兽争霸3冰封王座地图下载 返回列表
2019乌鲁木齐沐足论坛 北京pk10最稳定玩法 球探网足球即时此分 威龙网上娱乐 三分时时彩官方网站 pk10全天计划免费版 网上棋牌赌博通比牛牛 公开一肖 东莞沐足特殊服务 广东时时走势图百度百度贴吧 虎门酒店小姐 看牌牛牛外挂 球探即时比分 北京pk赛车每天稳计划 下载红码管家 时时彩期号对应码