⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fsimage.cs

📁 最好用的站点内容管理系统 全部源代码都有
💻 CS
📖 第 1 页 / 共 2 页
字号:
            bitmap.Dispose();
        }
        /// <summary>
        /// 生成文本图片
        /// </summary>
        public void GenerateTextPic()
        {
            if (_title == null || _title.Equals(""))
                throw new NullReferenceException("图片标题为空!");

            ImageFormat format = GetFormat();

            Font f = new Font(_familyname, _fontsize, _fontstyle, GraphicsUnit.Pixel);
            Bitmap _bitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
            Graphics _g = Graphics.FromImage(_bitmap);
            SizeF size = _g.MeasureString(_title, f);
            _g.Dispose();
            _bitmap.Dispose();
            double rate = size.Height / size.Width;
            if (_width <= 0 && _height <= 0)
            {
                _width = (int)size.Width;
                _height = (int)size.Height;
            }
            else if (_width > 0 && _height <= 0)
            {
                _height = (int)(_width * rate);
            }
            else if (_width <= 0 && _height > 0)
            {
                _width = (int)(_height / rate);
            }
            Bitmap bitmap = new Bitmap(_width, _height, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(bitmap);
            g.Clear(_bgcolor);
            if (_txtpos.X < 0)
                _txtpos.X = _width - size.Width;
            if (_txtpos.Y < 0)
                _txtpos.Y = _height - size.Height;
            Brush b = new SolidBrush(_forcecolor);
            g.DrawString(_title, f, b, _txtpos);
            g.Dispose();
            if (_filepath.IndexOf("\\") > 0)
            {
                string dir = _filepath.Substring(0, _filepath.LastIndexOf("\\"));
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
            }
            bitmap.Save(_filepath, format);
            bitmap.Dispose();
        }
        /// <summary>
        /// 按照指定的大小生成图片,并将原图绘制过去
        /// </summary>
        /// <param name="graphics"></param>
        /// <returns></returns>
        private Bitmap GetBitmap(out Graphics graphics)
        {
            //判断文件类型是否为图像类型
            Image image = Image.FromFile(_filepath);
            int x = image.Width;
            int y = image.Height;
            double rate = (float)y / (float)x;
            if (_width <= 0 && _height <= 0)
            {
                _width = x;
                _height = y;
            }
            else if (_width > 0 && _height <= 0)
            {
                _height = (int)(_width * rate);
            }
            else if (_width <= 0 && _height > 0)
            {
                _width = (int)(_height / rate);
            }
            Bitmap bitmap = new Bitmap(_width, _height, PixelFormat.Format32bppArgb);
            graphics = Graphics.FromImage(bitmap);
            graphics.Clear(_bgcolor);
            graphics.DrawImage(image, new Rectangle(0, 0, _width, _height));
            image.Dispose();
            return bitmap;
        }
        /// <summary>
        /// 根据文件扩展名取得图片的格式
        /// </summary>
        /// <returns></returns>
        private ImageFormat GetFormat()
        {
            if (_filepath == null || _filepath.IndexOf(".") < 0)
                throw new FileNotFoundException("文件路径不能为空或是文件名不正确!");
            string extend = _filepath.Substring(_filepath.LastIndexOf(".")).ToLower();
            switch (extend)
            {
                case ".jpe":
                    return ImageFormat.Jpeg;
                case ".jpeg":
                    return ImageFormat.Jpeg;
                case ".jpg":
                    return ImageFormat.Jpeg;
                case ".png":
                    return ImageFormat.Png;
                case ".tif":
                    return ImageFormat.Tiff;
                case ".tiff":
                    return ImageFormat.Tiff;
                case ".bmp":
                    return ImageFormat.Bmp;
                case ".gif":
                    return ImageFormat.Gif;
                default:
                    throw new FormatException("文件格式不受支持!");
            }
        }


        /// <summary>
        ///  生成图片水印
        /// </summary>
        public void WaterPicturemark()
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(_filepath);
            Bitmap b = new Bitmap(image.Width, image.Height,PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(b);

            g.Clear(Color.White);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.DrawImage(image, 0, 0, image.Width, image.Height);

            WaterPicturmark(g, image.Height, image.Width);
            image.Dispose();

            b.Save(_filepath);
            b.Dispose();
        }

        protected void WaterPicturmark(Graphics g, int height, int width)
        {
            Image watermark = new Bitmap(_waterpath);
            ImageAttributes imageAttributes = new ImageAttributes();
            ColorMap colorMap = new ColorMap();
            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
            ColorMap[] remapTable = { colorMap };
            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
            float[][] colorMatrixElements = {
                                                new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
                                                new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
                                                new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
                                                new float[] {0.0f,  0.0f,  0.0f,  0.3f, 0.0f},
                                                new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
                                            };
            ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            int xpos = 0;
            int ypos = 0;
            int WatermarkWidth = 0;
            int WatermarkHeight = 0;
            double bl = 1d;
            //计算水印图片的比率
            //取背景的1/4宽度来比较
            if ((width > watermark.Width * 4) && (height > watermark.Height * 4))
            {
                bl = 1;
            }
            else if ((width > watermark.Width * 4) && (height < watermark.Height * 4))
            {
                bl = Convert.ToDouble(height / 4) / Convert.ToDouble(watermark.Height);
            }
            else if ((width < watermark.Width * 4) && (height > watermark.Height * 4))
            {
                bl = Convert.ToDouble(width / 4) / Convert.ToDouble(watermark.Width);
            }
            else
            {
                if ((width * watermark.Height) > (_height * watermark.Width))
                {
                    bl = Convert.ToDouble(height / 4) / Convert.ToDouble(watermark.Height);
                }
                else
                {
                    bl = Convert.ToDouble(width / 4) / Convert.ToDouble(watermark.Width);
                }
            }

            WatermarkWidth = Convert.ToInt32(watermark.Width * bl);
            WatermarkHeight = Convert.ToInt32(watermark.Height * bl);

            switch (_waterpos)
            {
                case "1":
                    xpos = 10;
                    ypos = 10;
                    break;
                case "3":
                    xpos = width - WatermarkWidth - 10;
                    ypos = 10;
                    break;
                case "4":
                    xpos = width - WatermarkWidth - 10;
                    ypos = height - WatermarkHeight - 10;
                    break;
                case "2":
                    xpos = 10;
                    ypos = height - WatermarkHeight - 10;
                    break;
                default:
                    xpos = Convert.ToInt32(width / 2) - (WatermarkWidth / 2);
                    ypos = Convert.ToInt32(height / 2) - (WatermarkHeight / 2);
                    break;
            }
            g.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
            watermark.Dispose();
            imageAttributes.Dispose();
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -