以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  [讨论] FB如何让头像变灰?  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=32727)

--  作者:don
--  发布时间:2013/5/7 11:12:00
--  [讨论] FB如何让头像变灰?
百度来的头像变灰的方法,请高手转为FB可用的代码:

一.
procedure Gray(bmp: TBitmap);
var
  p: PByteArray;
  w: Integer;
  i, j: Integer;
begin
  bmp.pixelformat := pf24bit;
  for i := 0 to bmp.height - 1 do
  begin
    p := bmp.scanline[i];
    j := 0;
    while j < (bmp.width-1) * 3 do
    begin
      w :=(p[j] * 28 + p[j+1] * 151 + p[j+2]*77);
      w := w shr 8;
      p[j] := byte(w);
      p[j+1] := byte(w);
      p[j+2] := byte(w);
      inc(j, 3)
    end;
  end;
end;


二. 微软做好的,GDI+绘制代码:

Graphics graphics(hdc);
            Image myImage(L"Image.jpg");
            RectF source(0.0f, 0.0f, 330.0f, 300.0f);
            RectF destination(20.0f, 20.0f, 330.0f, 300.0f);
 
            DrawImage(graphics, &myImage, destination, source, UnitPixel, NULL); // 绘制原始图像
             
            RectF destination2(420.0f, 20.0f, 330.0f, 300.0f);
 
#define GRAY        32/256.0f    /* gray/full, 0 - 256 : black to white, 16 - 64 is recommended */
#define GRAY        0.125f        /* 0.000 - 1.000 : black to white, 0.062 - 0.250 is recommended */
#define FULL        1.000f
#define ZERO        0.000f
 
                        // 自由转换
            ColorMatrix matrix = {
                GRAY, GRAY, GRAY, FULL, ZERO, 
                GRAY, GRAY, GRAY, FULL, ZERO, 
                GRAY, GRAY, GRAY, FULL, ZERO, 
                GRAY, GRAY, GRAY, FULL, ZERO, 
                ZERO, ZERO, ZERO, ZERO, FULL, 
            };
 
            ImageAttributes attributes;
            attributes.SetColorMatrix(&matrix);
            DrawImage(graphics, &myImage, destination2, source, UnitPixel, &attributes); // 绘制转换图像



DrawImage 是自定义函数


C/C++ code


void DrawImage(Graphics& graphics, Image* image, RectF& destination, RectF& source, Unit unit, ImageAttributes* attributes)
{
    graphics.DrawImage(image, destination, source.X, source.Y, source.Width, source.Height, unit, attributes);
}



--  作者:blackzhu
--  发布时间:2013/5/7 11:57:00
--  
全局的apI 通不过哦


--  作者:Bin
--  发布时间:2013/5/7 12:02:00
--  
 Windows.Forms.ControlPaint.DrawImageDisabled(Graphics.FromImage(图像), 图像, 0, 0, Color.Transparent) 直接使用这段代码就可以的了.
--  作者:Bin
--  发布时间:2013/5/7 12:22:00
--  
如果希望用代码实现,(可控性比较高) 可以参考我的代码. 可以实现任意灰度,反色 颜色修改等.

Dim SImage As Image = Image.FromFile("C:\\Users\\foxtable1\\Desktop\\70b70d98-1228-4487-93a5-6f7056628d00.jpg")
Dim Height As Integer = SImage.Height
Dim Width  As Integer = SImage.Width
Dim bitmap As Bitmap = new Bitmap(Width, Height)
Dim MyBitmap As Bitmap= SImage
Dim pixel As Color
For  x As Integer = 0 To Width-1
    For  y As Integer = 0 To Height-1
        pixel = MyBitmap.GetPixel(x, y)
        Dim r As Integer = 0
        Dim g As Integer = 0
        Dim b As Integer = 0
        Dim Result As Integer = 0
        r = pixel.R
        g = pixel.G
        b = pixel.B
        Dim iType As Integer = 2
        Select Case iType
            Case 0 \'平均值法
                Result = ((r + g + b) / 3)
            Case 1 \'最大值法
                Result = IIF(r > g ,r , g)
                Result = IIF(Result > b,Result,b)
            Case 2 \'加权平均值法
                Result = ((0.7 * r) + (0.2 * g) + (0.1 * b))
        End Select
        bitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result))
    Next
Next
bitmap.Save("C:\\Users\\foxtable1\\Desktop\\28d00.jpg")

--  作者:qq121454970
--  发布时间:2013/5/7 12:24:00
--  
高手图片点击可在新窗口打开查看
--  作者:hanxuntx
--  发布时间:2013/5/7 12:28:00
--  

好贴收藏


--  作者:don
--  发布时间:2013/5/7 12:29:00
--  
高手动手了,学习!
--  作者:blackzhu
--  发布时间:2013/5/7 12:37:00
--  
保留着  学习
--  作者:blackzhu
--  发布时间:2013/5/7 13:08:00
--  
Windows.Forms.ControlPaint.DrawImageDisabled(Graphics.FromImage(图像), 图像, 0, 0, Color.Transparent)

完整的语法是怎样的?

--  作者:don
--  发布时间:2013/5/7 13:28:00
--  
同问,不知如何设置保存位置 ,

Dim SImage As Image = Image.FromFile("D:\\8d00.jpg")
Dim bcmage As Image = Image.SaveFile("D:\\800.jpg")   \' Image.SaveFile报错