• 大小: 8.74M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-04-25
  • 语言: C#
  • 标签: 亮度  自动  对比  图片  

资源简介

设置图片亮度,和对比度。可以自动纠偏

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace imagecalc
{
   public class CommonCalc
    {
        /// 
        /// 伽马 Gamma校正
        /// 

        /// 输入Bitmap
        /// [0 <-明- 1 -暗-> 2]
        /// 输出Bitmap
        public static Bitmap KiGamma(Bitmap bmp float val)
        {
            if (bmp == null)
            {
                return null;
            }

            // 1表示无变化,就不做
            if (val == 1.0000f) return bmp;

            try
            {
                Bitmap b = new Bitmap(bmp.Width bmp.Height);
                Graphics g = Graphics.FromImage(b);
                ImageAttributes attr = new ImageAttributes();

                attr.SetGamma(val ColorAdjustType.Bitmap);
                g.DrawImage(bmp new Rectangle(0 0 bmp.Width bmp.Height) 0 0 bmp.Width bmp.Height GraphicsUnit.Pixel attr);
                g.Dispose();
                return b;
            }
            catch
            {
                return null;
            }
        }

     

        /// 
        /// 图像对比度调整
        /// 

        /// 原始图
        /// 对比度[-100 100]
        /// 
        public static Bitmap KiContrast(Bitmap b int degree)
        {
            if (b == null)
            {
                return null;
            }

            //改成0-100,  50不变。

            if (degree < -100) degree = -100;
            if (degree > 100) degree = 100;

            try
            {

                double pixel = 0;
                double contrast = (100.0 + degree) / 100.0;
                contrast *= contrast;
                int width = b.Width;
                int height = b.Height;
                BitmapData data = b.LockBits(new Rectangle(0 0 width height) ImageLockMode.ReadWrite PixelFormat.Format24bppRgb);
                unsafe
                {
                    byte* p = (byte*)data.Scan0;
                    int offset = data.Stride - width * 3;
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            // 处理指定位置像素的对比度
                            for (int i = 0; i < 3; i++)
                            {
                                pixel = ((p[i] / 255.0 - 0.5) * contrast + 0.5) * 255;
                                if (pixel < 0) pixel = 0;
                                if (pixel > 255) pixel = 255;
                                p[i] = (byte)pixel;
                            } // i
                            p += 3;
                        } // x
                        p += offset;
   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        2946  2020-12-09 15:34  imagecalc.sln
     目录           0  2020-12-10 09:59  imagecalc\
     文件         189  2020-12-04 14:51  imagecalc\App.config
     目录           0  2020-12-04 14:51  imagecalc\bin\
     目录           0  2020-12-08 11:51  imagecalc\bin\Debug\
     文件      932747  2020-12-04 16:52  imagecalc\bin\Debug\1.jpg
     文件     1011091  2020-12-05 14:31  imagecalc\bin\Debug\2.jpg
     文件     1234985  2020-01-20 14:08  imagecalc\bin\Debug\3.jpg
     文件     1397640  2020-01-20 14:08  imagecalc\bin\Debug\4.jpg
     文件      372438  2020-11-30 16:51  imagecalc\bin\Debug\5.jpg
     文件      505480  2020-11-30 16:51  imagecalc\bin\Debug\6.jpg
     文件      540280  2020-11-30 16:51  imagecalc\bin\Debug\7.jpg
     文件      589616  2020-11-30 16:51  imagecalc\bin\Debug\8.jpg
     文件     1239013  2020-11-30 16:51  imagecalc\bin\Debug\9.jpg
     文件       24064  2020-12-10 09:59  imagecalc\bin\Debug\imagecalc.exe
     文件         189  2020-12-04 14:51  imagecalc\bin\Debug\imagecalc.exe.config
     文件       69120  2020-12-10 09:59  imagecalc\bin\Debug\imagecalc.pdb
     文件      920918  2020-12-05 14:51  imagecalc\bin\Debug\test.jpg
     文件      971723  2020-12-10 09:59  imagecalc\bin\Debug\纠偏1.jpg
     文件        4548  2020-12-09 11:28  imagecalc\CommonCalc.cs
     文件        5329  2020-12-04 15:03  imagecalc\Deskew.cs
     文件        1301  2020-12-04 16:56  imagecalc\deskew2.cs
     文件        9097  2020-12-04 16:56  imagecalc\DeskewOperat.cs
     文件        7956  2020-12-10 09:59  imagecalc\Form1.cs
     文件       14112  2020-12-10 09:59  imagecalc\Form1.Designer.cs
     文件        5817  2020-12-10 09:59  imagecalc\Form1.resx
     文件        5269  2020-12-04 16:56  imagecalc\gmseDeskew.cs
     文件        3855  2020-12-04 16:56  imagecalc\imagecalc.csproj
     目录           0  2020-12-04 14:51  imagecalc\obj\
     目录           0  2020-12-10 09:59  imagecalc\obj\Debug\
     文件        1443  2020-12-08 11:47  imagecalc\obj\Debug\DesignTimeResolveAssemblyReferences.cache
............此处省略20个文件信息

评论

共有 条评论