• 图像处理函数


    反相
    public static bool Invert(Bitmap b)
    {
              BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
              ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    int stride = bmData.Stride;
    System.IntPtr Scan0 = bmData.Scan0;
    unsafe
    {
    byte * p = (byte *)(void *)Scan0;
    int nOffset = stride - b.Width*3;
    int nWidth = b.Width * 3;
    for(int y=0;y < b.Height;++y)
              {
              for(int x=0; x < nWidth; ++x screen.width/2)this.width=screen.width/2" vspace=2 border=0>
              {
    p[0] = (byte)(255-p[0]);
    ++p;
    }
    p += nOffset;
    }
    }

    b.UnlockBits(bmData);
    return true;
    }


    public static bool Grayscale(Bitmap b)
    {
    //变成黑白
    BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
    ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    int stride = bmData.Stride;
    System.IntPtr Scan0 = bmData.Scan0;
    unsafe
    {
    byte * p = (byte *)(void *)Scan0;
    int nOffset = stride - b.Width*3;
    byte red, green, blue;
    for(int y=0;y < b.Height;++y)
    {
    for(int x=0; x < b.Width; ++x screen.width/2)this.width=screen.width/2" vspace=2 border=0>
    {
    blue = p[0];
    green = p[1];
    red = p[2];
    p[0] = p[1] = p[2] = (byte)(.299 * red
    + .587 * green
    + .114 * blue);
    p += 3;
    }
    p += nOffset;
    }
    }
    b.UnlockBits(bmData);
    return true;
    }

    亮度
    public static bool Brightness(Bitmap b,int nBrightness)
    {
    BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
    ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    int stride = bmData.Stride;
    System.IntPtr Scan0 = bmData.Scan0;
    unsafe
    {
    byte * p = (byte *)(void *)Scan0;
    int nOffset = stride - b.Width*3;
    int nWidth = b.Width * 3;
    for(int y=0;y<b.Height;++y)
    {
    for (int x = 0;   x < nWidth; ++x)
    {
    int nVal = (int) (p[0] + nBrightness);
    if (nVal < 0) nVal = 0;
    if (nVal > 255) nVal = 255;
    p[0] = (byte)nVal;
    ++p;
    }
    p += nOffset;
    }
    }
    b.UnlockBits(bmData);
    return true;
    }

    对比度
    public static bool Contrast(Bitmap b,int nContrast)
    {
    if (nContrast < -100) return false;
    if (nContrast >   100) return false;
    double pixel = 0, contrast = (100.0+nContrast)/100.0;
    contrast *= contrast;
    BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
    ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    int stride = bmData.Stride;
    System.IntPtr Scan0 = bmData.Scan0;
    unsafe
    {
    byte * p = (byte *)(void *)Scan0;
    int nOffset = stride - b.Width*3;
    int nWidth = b.Width * 3;
    for(int y=0;y<b.Height;++y)
    {
    for (int x = 0;   x < nWidth;++x)
    {
    byte color = p[0];    
    pixel = color/255.0;
    pixel -= 0.5;
    pixel *= contrast;
    pixel += 0.5;
    pixel *= 255;
    if (pixel < 0) pixel = 0;
    if (pixel > 255) pixel = 255;
    p[0] = (byte) pixel;
    ++p;
    }
    p += nOffset;
    }
    }
    b.UnlockBits(bmData);
    return true;
    }


    public static bool Gamma(Bitmap b)
    {
    BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
    ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    int stride = bmData.Stride;
    System.IntPtr Scan0 = bmData.Scan0;

    byte [] redGamma = new byte [256];
    byte [] greenGamma = new byte [256];
    byte [] blueGamma = new byte [256];

    for (int i = 0; i < 256; ++i)
    {
    redGamma[i] = (byte)Math.Min(255, (int)(( 255.0
    * Math.Pow(i/255.0, 1.0/5)) + 0.5));//其中1.0/5里的5是红色Gamma值。
    greenGamma[i] = (byte)Math.Min(255, (int)(( 255.0
    * Math.Pow(i/255.0, 1.0/5)) + 0.5));
    blueGamma[i] = (byte)Math.Min(255, (int)(( 255.0
    * Math.Pow(i/255.0, 1.0/5)) + 0.5));
    }

    unsafe
    {
    byte * p = (byte *)(void *)Scan0;
    int nOffset = stride - b.Width*3;
    int nWidth = b.Width * 3;

    byte red, green, blue;
    for(int y=0;y<b.Height;++y)
    {
    for (int x = 0;   x < b.Width;++x)
    {
    blue = p[0];
    green = p[1];
    red = p[2];

    p[0]=(byte)blueGamma[(int)blue];
    p[1]=(byte)greenGamma[(int)green];
    p[2]=(byte)redGamma[(int)red];
    p+=3;
    }
    p += nOffset;
    }
    }

    b.UnlockBits(bmData);
    return true;
    }

    通道
    public static bool Channel(Bitmap b,char rgb)
    {
    BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
    ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    int stride = bmData.Stride;
    System.IntPtr Scan0 = bmData.Scan0;

    unsafe
    {
    byte * p = (byte *)(void *)Scan0;
    int nOffset = stride - b.Width*3;
    int nWidth = b.Width * 3;

    byte red, green, blue;
    for(int y=0;y<b.Height;++y)
    {
    for (int x = 0;   x < b.Width;++x)
    {
    switch(rgb)
    {
    case 'r':
    p[0]=(byte)0;
    p[1]=(byte)0;
    break;
    case 'g':
    p[0]=(byte)0;
    p[2]=(byte)0;
    break;
    case 'b':
    p[2]=(byte)0;
    p[1]=(byte)0;
    break;
    }
    p+=3;
    }
    p += nOffset;
    }
    }

    b.UnlockBits(bmData);

    return true;
    }
  • 相关阅读:
    在windows下拆卸Linux就是这么俭朴
    打点Linux下永中Office和桌面殊效的冲突
    Banshee 0.11.4
    ubuntu8.0中文输入法
    RedFlag 6.0 硬盘安置我解
    阅读器和把持体系和用户的IQ
    初试Fedora,最后还是Xubuntu
    VMware中放置Ubuntu后鼠标滚轮标题问题办理
    GNOME 的文件经管器将片面支撑标签式阅读
    ATI显卡开启fedora9的3d后果的一些条记
  • 原文地址:https://www.cnblogs.com/benzhang/p/1458695.html
Copyright © 2020-2023  润新知