打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
delphi基本图像处理

delphi基本图像处理

时间:2011-5-26来源:yang 作者: peng点击: 436次

delphi基本图像处理代码
//浮雕
procedure emboss(srcbmp,destbmp:tbitmap;azimuthchange:integer);overload;
var
  i, j, gray, azimuthvalue, r, g, b: integer;
  srcrgb, srcrgb1, srcrgb2, destrgb: prgbtriple;
begin
  for i := 0 to srcbmp.height - 1 do
  begin
    srcrgb := srcbmp.scanline[i];
    destrgb := destbmp.scanline[i];
    if (azimuthchange >= -180) and (azimuthchange < -135) then
    begin
      if i > 0 then
        srcrgb1 := srcbmp.scanline[i-1]
      else
        srcrgb1 := srcrgb;
      inc(srcrgb1);
      srcrgb2 := srcrgb;
      inc(srcrgb2);
    end
    else if (azimuthchange >= -135) and (azimuthchange < -90) then
    begin
      if i > 0 then
        srcrgb1 := srcbmp.scanline[i-1]
      else
        srcrgb1 := srcrgb;
      srcrgb2 := srcrgb1;
      inc(srcrgb2);
    end
    else if (azimuthchange >= -90) and (azimuthchange < -45) then
    begin
      if i > 0 then
        srcrgb1 := srcbmp.scanline[i-1]
      else
        srcrgb1 := srcrgb;
      srcrgb2 := srcrgb1;
    end
    else if (azimuthchange >= -45) and (azimuthchange < 0) then
    begin
      srcrgb1 := srcrgb;
      if i > 0 then
        srcrgb2 := srcbmp.scanline[i-1]
      else
        srcrgb2 := srcrgb;
    end
    else if (azimuthchange >= 0) and (azimuthchange < 45) then
    begin
      srcrgb2 := srcrgb;
      if (i < srcbmp.height - 1) then
        srcrgb1 := srcbmp.scanline[i+1]
      else
        srcrgb1 := srcrgb;
    end
    else if (azimuthchange >= 45) and (azimuthchange < 90) then
    begin
      if (i < srcbmp.height - 1) then
        srcrgb1 := srcbmp.scanline[i+1]
      else
        srcrgb1 := srcrgb;
      srcrgb2 := srcrgb1;
    end
    else if (azimuthchange >= 90) and (azimuthchange < 135) then
    begin
      if (i < srcbmp.height - 1) then
        srcrgb1 := srcbmp.scanline[i+1]
      else
        srcrgb1 := srcrgb;
      srcrgb2 := srcrgb1;
      inc(srcrgb1);
    end
    else if (azimuthchange >= 135) and (azimuthchange <= 180) then
    begin
      if (i < srcbmp.height - 1) then
        srcrgb2 := srcbmp.scanline[i+1]
      else
        srcrgb2 := srcrgb;
      inc(srcrgb2);
      srcrgb1 := srcrgb;
      inc(srcrgb1);
    end;
    for j := 0 to srcbmp.width - 1 do
    begin
      if (azimuthchange >= -180) and (azimuthchange < -135) then
      begin
        azimuthvalue := azimuthchange + 180;
        r:=srcrgb.rgbtred-((srcrgb1.rgbtred)*azimuthvalue div 45)-((srcrgb2.rgbtred)*(45-azimuthvalue) div 45)+78;
        g:=srcrgb.rgbtgreen-((srcrgb1.rgbtgreen)*azimuthvalue div 45)-((srcrgb2.rgbtgreen)*(45-azimuthvalue) div 45)+78;
        b:=srcrgb.rgbtblue-((srcrgb1.rgbtblue)*azimuthvalue div 45)-((srcrgb2.rgbtblue)*(45-azimuthvalue) div 45)+78;
      end
      else if (azimuthchange >= -135) and (azimuthchange < -90) then
      begin
        azimuthvalue := azimuthchange + 135;
        r:=srcrgb.rgbtred-((srcrgb1.rgbtred)*azimuthvalue div 45)-((srcrgb2.rgbtred)*(45-azimuthvalue) div 45)+78;
        g:=srcrgb.rgbtgreen-((srcrgb1.rgbtgreen)*azimuthvalue div 45)-((srcrgb2.rgbtgreen)*(45-azimuthvalue) div 45)+78;
        b:=srcrgb.rgbtblue-((srcrgb1.rgbtblue)*azimuthvalue div 45)-((srcrgb2.rgbtblue)*(45-azimuthvalue) div 45)+78;
      end
      else if (azimuthchange >= -90) and (azimuthchange < -45) then
      begin
        if j=1 then inc(srcrgb1,-1);
        azimuthvalue := azimuthchange + 90;
        r:=srcrgb.rgbtred-((srcrgb1.rgbtred)*azimuthvalue div 45)-((srcrgb2.rgbtred)*(45-azimuthvalue) div 45)+78;
        g:=srcrgb.rgbtgreen-((srcrgb1.rgbtgreen)*azimuthvalue div 45)-((srcrgb2.rgbtgreen)*(45-azimuthvalue) div 45)+78;
        b:=srcrgb.rgbtblue-((srcrgb1.rgbtblue)*azimuthvalue div 45)-((srcrgb2.rgbtblue)*(45-azimuthvalue) div 45)+78;
      end
      else if (azimuthchange >= -45) and (azimuthchange < 0) then
      begin
        if j=1 then
        begin
          inc(srcrgb1,-1);
          inc(srcrgb2,-1);
        end;
        azimuthvalue := azimuthchange + 45;
        r:=srcrgb.rgbtred-((srcrgb1.rgbtred)*azimuthvalue div 45)-((srcrgb2.rgbtred)*(45-azimuthvalue) div 45)+78;
        g:=srcrgb.rgbtgreen-((srcrgb1.rgbtgreen)*azimuthvalue div 45)-((srcrgb2.rgbtgreen)*(45-azimuthvalue) div 45)+78;
        b:=srcrgb.rgbtblue-((srcrgb1.rgbtblue)*azimuthvalue div 45)-((srcrgb2.rgbtblue)*(45-azimuthvalue) div 45)+78;
      end
      else if (azimuthchange >= 0) and (azimuthchange < 45) then
      begin
        if j=1 then
        begin
          inc(srcrgb1,-1);
          inc(srcrgb2,-1);
        end;
        azimuthvalue := azimuthchange;
        r:=srcrgb.rgbtred-((srcrgb1.rgbtred)*azimuthvalue div 45)-((srcrgb2.rgbtred)*(45-azimuthvalue) div 45)+78;
        g:=srcrgb.rgbtgreen-((srcrgb1.rgbtgreen)*azimuthvalue div 45)-((srcrgb2.rgbtgreen)*(45-azimuthvalue) div 45)+78;
        b:=srcrgb.rgbtblue-((srcrgb1.rgbtblue)*azimuthvalue div 45)-((srcrgb2.rgbtblue)*(45-azimuthvalue) div 45)+78;
      end
      else if (azimuthchange >= 45) and (azimuthchange < 90) then
      begin
        if j=1 then inc(srcrgb2,-1);
        azimuthvalue := azimuthchange - 45;
        r:=srcrgb.rgbtred-((srcrgb1.rgbtred)*azimuthvalue div 45)-((srcrgb2.rgbtred)*(45-azimuthvalue) div 45)+78;
        g:=srcrgb.rgbtgreen-((srcrgb1.rgbtgreen)*azimuthvalue div 45)-((srcrgb2.rgbtgreen)*(45-azimuthvalue) div 45)+78;
        b:=srcrgb.rgbtblue-((srcrgb1.rgbtblue)*azimuthvalue div 45)-((srcrgb2.rgbtblue)*(45-azimuthvalue) div 45)+78;
      end
      else if (azimuthchange >= 90) and (azimuthchange < 135) then
      begin
        azimuthvalue := azimuthchange - 90;
        r:=srcrgb.rgbtred-((srcrgb1.rgbtred)*azimuthvalue div 45)-((srcrgb2.rgbtred)*(45-azimuthvalue) div 45)+78;
        g:=srcrgb.rgbtgreen-((srcrgb1.rgbtgreen)*azimuthvalue div 45)-((srcrgb2.rgbtgreen)*(45-azimuthvalue) div 45)+78;
        b:=srcrgb.rgbtblue-((srcrgb1.rgbtblue)*azimuthvalue div 45)-((srcrgb2.rgbtblue)*(45-azimuthvalue) div 45)+78;
      end
      else if (azimuthchange >= 135) and (azimuthchange <= 180) then
      begin
        azimuthvalue := azimuthchange - 135;
        r:=srcrgb.rgbtred-((srcrgb1.rgbtred)*azimuthvalue div 45)-((srcrgb2.rgbtred)*(45-azimuthvalue) div 45)+78;
        g:=srcrgb.rgbtgreen-((srcrgb1.rgbtgreen)*azimuthvalue div 45)-((srcrgb2.rgbtgreen)*(45-azimuthvalue) div 45)+78;
        b:=srcrgb.rgbtblue-((srcrgb1.rgbtblue)*azimuthvalue div 45)-((srcrgb2.rgbtblue)*(45-azimuthvalue) div 45)+78;
      end;
      r:=min(r,255);
      r:=max(r,0);
      g:=min(g,255);
      g:=max(g,0);
      b:=min(b,255);
      b:=max(b,0);
      gray := (r shr 2) + (r shr 4) + (g shr 1) + (g shr 4) + (b shr 3);
      destrgb.rgbtred:=gray;
      destrgb.rgbtgreen:=gray;
      destrgb.rgbtblue:=gray;
      if (j=-180) and (azimuthchange<-135)) or ((azimuthchange>=90) and (azimuthchange<=180))) then
      begin
        inc(srcrgb1);
      end;
      if (j=135) and (azimuthchange<180)) or ((azimuthchange>=-180) and (azimuthchange<=-90))) then
      begin
        inc(srcrgb2);
      end;
      inc(srcrgb);
      inc(destrgb);
    end;
  end;
end;
procedure emboss(bmp:tbitmap;azimuthchange:integer;elevationchange:integer;weightchange:integer);overload;
var
  destbmp:tbitmap;
begin
  destbmp:=tbitmap.create;
  destbmp.assign(bmp);
  emboss(bmp,destbmp,azimuthchange,elevationchange,weightchange);
  bmp.assign(destbmp);
end;
//反色
procedure negative(bmp:tbitmap);
var
  i, j: integer;
  prgb: prgbtriple;
begin
  bmp.pixelformat:=pf24bit;
  for i := 0 to bmp.height - 1 do
  begin
    prgb := bmp.scanline[i];
    for j := 0 to bmp.width - 1 do
    begin
      prgb^.rgbtred :=not prgb^.rgbtred ;
      prgb^.rgbtgreen :=not prgb^.rgbtgreen;
      prgb^.rgbtblue :=not prgb^.rgbtblue;
      inc(prgb);
    end;
  end;
end;
//曝光
procedure exposure(bmp:tbitmap);
var
  i, j: integer;
  prgb: prgbtriple;
begin
  bmp.pixelformat:=pf24bit;
  for i := 0 to bmp.height - 1 do
  begin
    prgb := bmp.scanline[i];
    for j := 0 to bmp.width - 1 do
    begin
      if prgb^.rgbtred<128 then
        prgb^.rgbtred :=not prgb^.rgbtred ;
      if prgb^.rgbtgreen<128 then
        prgb^.rgbtgreen :=not prgb^.rgbtgreen;
      if prgb^.rgbtblue<128 then
        prgb^.rgbtblue :=not prgb^.rgbtblue;
      inc(prgb);
    end;
  end;
end;
//模糊
procedure blur(srcbmp:tbitmap);
var
  i, j:integer;
  srcrgb:prgbtriple;
  srcnextrgb:prgbtriple;
  srcprergb:prgbtriple;
  value:integer;
  procedure incrgb;
  begin
    inc(srcprergb);
    inc(srcrgb);
    inc(srcnextrgb);
  end;
  procedure decrgb;
  begin
    inc(srcprergb,-1);
    inc(srcrgb,-1);
    inc(srcnextrgb,-1);
  end;
begin
  srcbmp.pixelformat:=pf24bit;
  for i := 0 to srcbmp.height - 1 do
  begin
    if i > 0 then
      srcprergb:=srcbmp.scanline[i-1]
    else
      srcprergb := srcbmp.scanline[i];
    srcrgb := srcbmp.scanline[i];
    if i < srcbmp.height - 1 then
      srcnextrgb:=srcbmp.scanline[i+1]
    else
      srcnextrgb:=srcbmp.scanline[i];
    for j := 0 to srcbmp.width - 1 do
    begin
      if j > 0 then decrgb;
      value:=srcprergb.rgbtred+srcrgb.rgbtred+srcnextrgb.rgbtred;
      if j > 0 then incrgb;
      value:=value+srcprergb.rgbtred+srcrgb.rgbtred+srcnextrgb.rgbtred;
      if j < srcbmp.width - 1 then incrgb;
      value:=(value+srcprergb.rgbtred+srcrgb.rgbtred+srcnextrgb.rgbtred) div 9;
      decrgb;
      srcrgb.rgbtred:=value;
      if j > 0 then decrgb;
      value:=srcprergb.rgbtgreen+srcrgb.rgbtgreen+srcnextrgb.rgbtgreen;
      if j > 0 then incrgb;
      value:=value+srcprergb.rgbtgreen+srcrgb.rgbtgreen+srcnextrgb.rgbtgreen;
      if j < srcbmp.width - 1 then incrgb;
      value:=(value+srcprergb.rgbtgreen+srcrgb.rgbtgreen+srcnextrgb.rgbtgreen) div 9;
      decrgb;
      srcrgb.rgbtgreen:=value;
      if j > 0 then decrgb;
      value:=srcprergb.rgbtblue+srcrgb.rgbtblue+srcnextrgb.rgbtblue;
      if j > 0 then incrgb;
      value:=value+srcprergb.rgbtblue+srcrgb.rgbtblue+srcnextrgb.rgbtblue;
      if j < srcbmp.width - 1 then incrgb;
      value:=(value+srcprergb.rgbtblue+srcrgb.rgbtblue+srcnextrgb.rgbtblue) div 9;
      decrgb;
      srcrgb.rgbtblue:=value;
      incrgb;
    end;
  end;
end;
//锐化
procedure sharpen(srcbmp:tbitmap);
var
  i, j: integer;
  srcrgb: prgbtriple;
  srcprergb: prgbtriple;
  value: integer;
begin
  srcbmp.pixelformat:=pf24bit;
  for i := 0 to srcbmp.height - 1 do
  begin
    srcrgb := srcbmp.scanline[i];
    if i > 0 then
      srcprergb:=srcbmp.scanline[i-1]
    else
      srcprergb:=srcbmp.scanline[i];
    for j := 0 to srcbmp.width - 1 do
    begin
      if j = 1 then dec(srcprergb);
      value:=srcrgb.rgbtred+(srcrgb.rgbtred-srcprergb.rgbtred) div 2;
      value:=max(0,value);
      value:=min(255,value);
      srcrgb.rgbtred:=value;
      value:=srcrgb.rgbtgreen+(srcrgb.rgbtgreen-srcprergb.rgbtgreen) div 2;
      value:=max(0,value);
      value:=min(255,value);
      srcrgb.rgbtgreen:=value;
      value:=srcrgb.rgbtblue+(srcrgb.rgbtblue-srcprergb.rgbtblue) div 2;
      value:=max(0,value);
      value:=min(255,value);
      srcrgb.rgbtblue:=value;
      inc(srcrgb);
      inc(srcprergb);
    end;
  end;
end;
 [图像的旋转和翻转]
以下代码用scanline配合指针移动实现,用于24位色!
//旋转90度
procedure rotate90(const bitmap:tbitmap);
var
  i,j:integer;
  rowin,rowout:prgbtriple;
  bmp:tbitmap;
  width,height:integer;
begin
  bmp:=tbitmap.create;
  bmp.width := bitmap.height;
  bmp.height := bitmap.width;
  bmp.pixelformat := pf24bit;
  width:=bitmap.width-1;
  height:=bitmap.height-1;
  for  j := 0 to height do
  begin
    rowin  := bitmap.scanline[j];
    for i := 0 to width do
    begin
      rowout := bmp.scanline[i];
      inc(rowout,height - j);
      rowout^ := rowin^;
      inc(rowin);
    end;
  end;
  bitmap.assign(bmp);
end;
//旋转180度
procedure rotate180(const bitmap:tbitmap);
var
  i,j:integer;
  rowin,rowout:prgbtriple;
  bmp:tbitmap;
  width,height:integer;
begin
  bmp:=tbitmap.create;
  bmp.width := bitmap.width;
  bmp.height := bitmap.height;
  bmp.pixelformat := pf24bit;
  width:=bitmap.width-1;
  height:=bitmap.height-1;
  for  j := 0 to height do
  begin
    rowin  := bitmap.scanline[j];
    for i := 0 to width do
    begin
      rowout := bmp.scanline[height - j];
      inc(rowout,width - i);
      rowout^ := rowin^;
      inc(rowin);
    end;
  end;
  bitmap.assign(bmp);
end;
//旋转270度
procedure rotate270(const bitmap:tbitmap);
var
  i,j:integer;
  rowin,rowout:prgbtriple;
  bmp:tbitmap;
  width,height:integer;
begin
  bmp:=tbitmap.create;
  bmp.width := bitmap.height;
  bmp.height := bitmap.width;
  bmp.pixelformat := pf24bit;
  width:=bitmap.width-1;
  height:=bitmap.height-1;
  for  j := 0 to height do
  begin
    rowin  := bitmap.scanline[j];
    for i := 0 to width do
    begin
      rowout := bmp.scanline[width - i];
      inc(rowout,j);
      rowout^ := rowin^;
      inc(rowin);
    end;
  end;
  bitmap.assign(bmp);
end;
//任意角度
function rotatebitmap(bitmap:tbitmap;angle:integer;backcolor:tcolor):tbitmap;
var
  i,j,ioriginal,joriginal,cospoint,sinpoint : integer;
  roworiginal,rowrotated : prgbtriple;
  sintheta,costheta : extended;
  angleadd : integer;
begin
  result:=tbitmap.create;
  result.pixelformat := pf24bit;
  result.canvas.brush.color:=backcolor;
  angle:=angle mod 360;
  if angle<0 then angle:=360-abs(angle);
  if angle=0 then
    result.assign(bitmap)
  else if angle=90 then
  begin
    result.assign(bitmap);
    rotate90(result);//如果是旋转90度,直接调用上面的代码
  end
  else if (angle>90) and (angle<180) then
  begin
    angleadd:=90;
    angle:=angle-angleadd;
  end
  else if angle=180 then
  begin
    result.assign(bitmap);
    rotate180(result);//如果是旋转180度,直接调用上面的过程
  end
  else if (angle>180) and (angle<270) then
  begin
    angleadd:=180;
    angle:=angle-angleadd;
  end
  else if angle=270 then
  begin
    result.assign(bitmap);
    rotate270(result);//如果是旋转270度,直接调用上面的过程
  end
  else if (angle>270) and (angle<360) then
  begin
    angleadd:=270;
    angle:=angle-angleadd;
  end
  else
    angleadd:=0;
  if (angle>0) and (angle<90) then
  begin
  sincos((angle + angleadd) * pi / 180, sintheta, costheta);
  if (sintheta * costheta) < 0 then
  begin
    result.width := round(abs(bitmap.width * costheta - bitmap.height * sintheta));
    result.height := round(abs(bitmap.width * sintheta - bitmap.height * costheta));
  end
  else
  begin
    result.width := round(abs(bitmap.width * costheta + bitmap.height * sintheta));
    result.height := round(abs(bitmap.width * sintheta + bitmap.height * costheta));
  end;
  costheta:=abs(costheta);
  sintheta:=abs(sintheta);
  if (angleadd=0) or (angleadd=180) then
  begin
    cospoint:=round(bitmap.height*costheta);
    sinpoint:=round(bitmap.height*sintheta);
  end
  else
  begin
    sinpoint:=round(bitmap.width*costheta);
    cospoint:=round(bitmap.width*sintheta);
  end;
  for j := 0 to result.height-1 do
  begin
    rowrotated := result.scanline[j];
    for i := 0 to result.width-1 do
    begin
      case angleadd of
        0:
        begin
          joriginal := round((j+1)*costheta-(i+1-sinpoint)*sintheta)-1;
          ioriginal := round((i+1)*costheta-(cospoint-j-1)*sintheta)-1;
        end;
        90:
        begin
          ioriginal := round((j+1)*sintheta-(i+1-sinpoint)*costheta)-1;
          joriginal := bitmap.height-round((i+1)*sintheta-(cospoint-j-1)*costheta);
        end;
        180:
        begin
          joriginal := bitmap.height-round((j+1)*costheta-(i+1-sinpoint)*sintheta);
          ioriginal := bitmap.width-round((i+1)*costheta-(cospoint-j-1)*sintheta);
        end;
        270:
        begin
          ioriginal := bitmap.width-round((j+1)*sintheta-(i+1-sinpoint)*costheta);
          joriginal := round((i+1)*sintheta-(cospoint-j-1)*costheta)-1;
        end;
      end;
      if (ioriginal >= 0) and (ioriginal <= bitmap.width-1)and
         (joriginal >= 0) and (joriginal <= bitmap.height-1)
      then
      begin
        roworiginal := bitmap.scanline[joriginal];
        inc(roworiginal,ioriginal);
        rowrotated^ := roworiginal^;
        inc(rowrotated);
      end
      else
      begin
        inc(rowrotated);
      end;
    end;
  end;
  end;
end;
//水平翻转
procedure fliphorz(const bitmap:tbitmap);
var
  i,j:integer;
  rowin,rowout:prgbtriple;
  bmp:tbitmap;
  width,height:integer;
begin
  bmp:=tbitmap.create;
  bmp.width := bitmap.width;
  bmp.height := bitmap.height;
  bmp.pixelformat := pf24bit;
  width:=bitmap.width-1;
  height:=bitmap.height-1;
  for  j := 0 to height do
  begin
    rowin  := bitmap.scanline[j];
    for i := 0 to width do
    begin
      rowout := bmp.scanline[j];
      inc(rowout,width - i);
      rowout^ := rowin^;
      inc(rowin);
    end;
  end;
  bitmap.assign(bmp);
end;
//垂直翻转
procedure flipvert(const bitmap:tbitmap);
var
  i,j:integer;
  rowin,rowout:prgbtriple;
  bmp:tbitmap;
  width,height:integer;
begin
  bmp:=tbitmap.create;
  bmp.width := bitmap.height;
  bmp.height := bitmap.width;
  bmp.pixelformat := pf24bit;
  width:=bitmap.width-1;
  height:=bitmap.height-1;
  for  j := 0 to height do
  begin
    rowin  := bitmap.scanline[j];
    for i := 0 to width do
    begin
      rowout := bmp.scanline[height - j];
      inc(rowout,i);
      rowout^ := rowin^;
      inc(rowin);
    end;
  end;
  bitmap.assign(bmp);
end;
[亮度、对比度、饱和度的调整]
以下代码用scanline配合指针移动实现!
function min(a, b: integer): integer;
begin
  if a < b then
    result := a
  else
    result := b;
end;
function max(a, b: integer): integer;
begin
  if a > b then
    result := a
  else
    result := b;
end;
//亮度调整
procedure brightnesschange(const srcbmp,destbmp:tbitmap;valuechange:integer);
var
  i, j: integer;
  srcrgb, destrgb: prgbtriple;
begin
  for i := 0 to srcbmp.height - 1 do
  begin
    srcrgb := srcbmp.scanline[i];
    destrgb := destbmp.scanline[i];
    for j := 0 to srcbmp.width - 1 do
    begin
      if valuechange > 0 then
      begin
        destrgb.rgbtred := min(255, srcrgb.rgbtred + valuechange);
        destrgb.rgbtgreen := min(255, srcrgb.rgbtgreen + valuechange);
        destrgb.rgbtblue := min(255, srcrgb.rgbtblue + valuechange);
      end else begin
        destrgb.rgbtred := max(0, srcrgb.rgbtred + valuechange);
        destrgb.rgbtgreen := max(0, srcrgb.rgbtgreen + valuechange);
        destrgb.rgbtblue := max(0, srcrgb.rgbtblue + valuechange);
      end;
      inc(srcrgb);
      inc(destrgb);
    end;
  end;
end;
//对比度调整
procedure contrastchange(const srcbmp,destbmp:tbitmap;valuechange:integer);
var
  i, j: integer;
  srcrgb, destrgb: prgbtriple;
begin
  for i := 0 to srcbmp.height - 1 do
  begin
    srcrgb := srcbmp.scanline[i];
    destrgb := destbmp.scanline[i];
    for j := 0 to srcbmp.width - 1 do
    begin
      if valuechange>=0 then
      begin
      if srcrgb.rgbtred >= 128 then
        destrgb.rgbtred := min(255, srcrgb.rgbtred + valuechange)
      else
        destrgb.rgbtred := max(0, srcrgb.rgbtred - valuechange);
      if srcrgb.rgbtgreen >= 128 then
        destrgb.rgbtgreen := min(255, srcrgb.rgbtgreen + valuechange)
      else
        destrgb.rgbtgreen := max(0, srcrgb.rgbtgreen - valuechange);
      if srcrgb.rgbtblue >= 128 then
        destrgb.rgbtblue := min(255, srcrgb.rgbtblue + valuechange)
      else
        destrgb.rgbtblue := max(0, srcrgb.rgbtblue - valuechange);
      end
      else
      begin
      if srcrgb.rgbtred >= 128 then
        destrgb.rgbtred := max(128, srcrgb.rgbtred + valuechange)
      else
        destrgb.rgbtred := min(128, srcrgb.rgbtred - valuechange);
      if srcrgb.rgbtgreen >= 128 then
        destrgb.rgbtgreen := max(128, srcrgb.rgbtgreen + valuechange)
      else
        destrgb.rgbtgreen := min(128, srcrgb.rgbtgreen - valuechange);
      if srcrgb.rgbtblue >= 128 then
        destrgb.rgbtblue := max(128, srcrgb.rgbtblue + valuechange)
      else
        destrgb.rgbtblue := min(128, srcrgb.rgbtblue - valuechange);
      end;
      inc(srcrgb);
      inc(destrgb);
    end;
  end;
end;
//饱和度调整
procedure saturationchange(const srcbmp,destbmp:tbitmap;valuechange:integer);
var
  grays: array[0..767] of integer;
  alpha: array[0..255] of word;
  gray, x, y: integer;
  srcrgb,destrgb: prgbtriple;
  i: byte;
begin
valuechange:=valuechange+255;
for i := 0 to 255 do
  alpha[i] := (i * valuechange) shr 8;
x := 0;
for i := 0 to 255 do
begin
  gray := i - alpha[i];
  grays[x] := gray;
  inc(x);
  grays[x] := gray;
  inc(x);
  grays[x] := gray;
  inc(x);
end;
for y := 0 to srcbmp.height - 1 do
begin
  srcrgb := srcbmp.scanline[y];
  destrgb := destbmp.scanline[y];
  for x := 0 to srcbmp.width - 1 do
  begin
    gray := grays[srcrgb.rgbtred + srcrgb.rgbtgreen + srcrgb.rgbtblue];
    if gray + alpha[srcrgb.rgbtred]>0 then
      destrgb.rgbtred := min(255,gray + alpha[srcrgb.rgbtred])
    else
      destrgb.rgbtred := 0;
    if gray + alpha[srcrgb.rgbtgreen]>0 then
      destrgb.rgbtgreen := min(255,gray + alpha[srcrgb.rgbtgreen])
    else
      destrgb.rgbtgreen := 0;
    if gray + alpha[srcrgb.rgbtblue]>0 then
      destrgb.rgbtblue := min(255,gray + alpha[srcrgb.rgbtblue])
    else
      destrgb.rgbtblue := 0;
    inc(srcrgb);
    inc(destrgb);
  end;
end;
end;
//rgb调整
procedure rgbchange(srcbmp,destbmp:tbitmap;redchange,greenchange,bluechange:integer);
var
  srcrgb, destrgb: prgbtriple;
  i,j:integer;
begin
  for i := 0 to srcbmp.height- 1 do
  begin
    srcrgb := srcbmp.scanline[i];
    destrgb :=destbmp.scanline[i];
    for j := 0 to srcbmp.width - 1 do
    begin
      if redchange> 0 then
        destrgb.rgbtred := min(255, srcrgb.rgbtred + redchange)
      else
        destrgb.rgbtred := max(0, srcrgb.rgbtred + redchange);
      if greenchange> 0 then
        destrgb.rgbtgreen := min(255, srcrgb.rgbtgreen + greenchange)
      else
        destrgb.rgbtgreen := max(0, srcrgb.rgbtgreen + greenchange);
      if bluechange> 0 then
        destrgb.rgbtblue := min(255, srcrgb.rgbtblue + bluechange)
      else
        destrgb.rgbtblue := max(0, srcrgb.rgbtblue + bluechange);
      inc(srcrgb);
      inc(destrgb);
    end;
  end;
end;
[颜色调整]
//rgb<=>bgr
procedure rgb2bgr(const bitmap:tbitmap);
var
  x: integer;
  y: integer;
  prgb: prgbtriple;
  color: byte;
begin
  for y := 0 to (bitmap.height - 1) do
  begin
    for x := 0 to (bitmap.width - 1) do
    begin
      color := prgb^.rgbtred;
      prgb^.rgbtred := prgb^.rgbtblue;
      prgb^.rgbtblue := color;
      inc(prgb);
    end;
    end
  end;
end;
//灰度化(加权)
procedure grayscale(const bitmap:tbitmap);
var
  x: integer;
  y: integer;
  prgb: prgbtriple;
  gray: byte;
begin
  for y := 0 to (bitmap.height - 1) do
  begin
    prgb := bitmap.scanline[y];
    for x := 0 to (bitmap.width - 1) do
    begin
      gray := (77 * red + 151 * green + 28 * blue) shr 8;
      prgb^.rgbtred:=gray;
      prgb^.rgbtgreen:=gray;
      prgb^.rgbtblue:=gray;
      inc(prgb);
    end;
  end;
end;
理论篇:
关键词:
绘图区-即窗口显示图像的区域,亦可为全屏幕(在全屏幕下绘图的效果比一般窗口下好)
中心点-即要绘图区显示的中心点在原始图像的坐标(声明:这个概念特别重要)
  先说说图像的放大,要放大一张图片,我们一般的做法是直接放大图像,但本文介绍的方法仅放大我们能够看到的部分,放大分两种情况,一种是放大后比绘图区还要小,这种情况没什么好说,当然是显示全部的图像;第二种是放大后的图像比绘图区大,这才是我们今天要讨论的重点话题,这种情况下我们先要确定图像放大后的大小,然后根据“中心点”计算在原始图像的位置和大小,最后把截取的图像放大到绘图区。
  再说说图像的漫游,当显示的图像超过绘图区时,我们需要对图像进行漫游,以便看到全部的图像。原理是:当鼠标在绘图区进行单击时,这时开始漫游,先记录鼠标的单击位置,然后检测鼠标的移动,根据鼠标和上次的位移计算出“中心点”(需要将屏幕坐标转换为原始图像坐标),根据在上面放大的原理到原始图像中取出要显示的部分,放大显示到绘图区。
算法实现篇:
1.图像放大
变量定义:
pzoom:放大率(整数:100时为100%,根据需要可以将 100 该为 10000 或者更大些,但不推荐使用浮点数)
a,b:中心点
w,h:要截取原始图像的宽和高
x,y:要截取的位置(左上角)
sw,sh:原始图像的宽和高
p1,p2:放大比例
aw,ah:放大后图像的大小
pw,ph:绘图区大小
vx,vy:在绘图区显示的位置(左上角)
vw,vh:在绘图区显示的大小
ptx,pty:临时变量
已知的变量:pzoom,(a,b),(sw,sh),(p1,p2),(aw,ah),(pw,ph)
要计算的变量:(x,y),(w,h),(vx,vy),(vw,vh)
开始计算:
aw=round(pzoom*sw/100);
ah=round(pzoom*sh/100);
p1=aw/pw
p2=ah/ph
// 注:round 用于取整,如其他语言的int(),fix()等
if p1>1 then w=round(sw/p1) else w=sw
if p2>1 then h=round(sh/p2) else h=sh
// 注:shr 为右移运算符,可以使用“>>1”、“div 2”、“\2”或“round(w/2)”代替
x=a-w shr 1
y=b-h shr 1
// 注:div 为整除运算符
ptx=(w*pzoom) div 100
pty=(h*pzoom) div 100
// 以下计算在绘图区显示的图像大小和位置
变量
    pencent:double;  // 缩放比
    wx:double;       // 宽缩放比
    hx:double;       // 高缩放比
    // 获得缩放比
    wx:=pw/ptx
    hx:=ph/pty
    if wx>hx then pencent:=hx
    else          pencent:=wx;
    // 获得图片最后的大小
    vw:=round(pencent*ptx);
    vh:=round(pencent*pty);
    // 计算出图片的位置
    vx:=(pw-vw) div 2;
    vy:=(ph-vh) div 2;
// ------------------------------------
好了,两个重要的任务完成(x,y),(w,h),(vx,vy),(vw,vh)已经全部计算得出,下面的工作就是显示了,我们选择 windows api 进行操作
// 以下显示图像 -----------------------
变量
sdc 为原始图片的设备句柄(dc)
tdc 为临时设备句柄
ddc 最终设备句柄
bitblt(tdc,0,0,w,h,sdc,0,0,srccopy);
setstretchbltmode(ddc,stretch_deletescans);
stretchblt(ddc,0,0,vw,vh,tdc,0,0,w,h,srccopy);
最后绘制到显示的区域即可:
例如:bitblt(getdc(0),vx,vy,vx+vw,xy+vh,ddc,0,0,srccopy);
// ------------------------------------
2.图像漫游
先定义三个全局变量:
fbegindragpoint   :tpoint;         // 记录鼠标开始拖动的位置
fbegindragsbpoint :tpoint;         // 记录“中心点”位置
fbegindrag        :boolean;        // 是否已经开始“拖动”
a,b               :integer;        // “中心点”位置
在鼠标左键点击时,记录鼠标的位置和“中心点”的位置,同时设置 fbegindrag 为真
当鼠标右键弹起时,设置 fbegindrag 为假
鼠标移动时,判断 fbegindrag ,如果为假不进行处理,如果为真进行下面处理:
假设 x,y 为鼠标当前的位置
a=fbegindragpoint.x-((x-fbegindragpoint.x)*100) div pzoom
b=fbegindragpoint.y-((y-fbegindragpoint.y)*100) div pzoom
最后使用上面介绍的图像放大显示出图像
技巧篇:
1.如果图像较大,使用 delphi 的 位图对象会出现内存溢出错误,这时可以进行如下设置:
    bitimage:=tbitmap.create;
    bitimage.pixelformat:=pf24bit;
    bitimage.releasehandle;
2.如果要让图像自动适应窗口的大小,参考以下代码:
var
    p1,p2       :double;
begin
    p1:=pw/sw;
    p2:=ph/sw;
    if p1>p2 then pzoom:=round(p2*100)
    else          pzoom:=round(p1*100);
    if pzoom=0 then pzoom:=100;
end;
delphi灰度图像像素颜色亮度处理
  在图像处理中,速度是很重要的。因此,我们得重新处理一下tbitmap,得到tvczhbitmap。这只是因为getpixels和setpixels的速度太慢,换一个方法而已。
  unit untbitmapproc;
  interface
  uses graphics, sysutils;
  type
  tvczhbitmap=class(tbitmap)
  private
  data:pbytearray;
  line:integer;
  procedure setformat;
  function getbytepointer(x,y:integer):pbyte;
  procedure setbytes(x,y:integer;value:byte);
  function getbytes(x,y:integer):byte;
  protected
  published
  constructor create;
  public
  property bytes[x,y:integer]:byte read getbytes write setbytes;
  procedure loadfromfile(filename:string);
  procedure togray;
  end;
  implementation
  procedure tvczhbitmap.setformat;
  begin
  handletype:=bmdib;
  pixelformat:=pf24bit;
  end;
  function tvczhbitmap.getbytepointer(x,y:integer):pbyte;
  begin
  if line<>y then
  begin
  line:=y;
  data:=scanline[y];
  end;
  longint(result):=longint(data)+x;
  end;
  procedure tvczhbitmap.setbytes(x,y:integer;value:byte);
  begin
  getbytepointer(x,y)^:=value;
  end;
  function tvczhbitmap.getbytes(x,y:integer):byte;
  begin
  result:=getbytepointer(x,y)^;
  end;
  constructor tvczhbitmap.create;
  begin
  inherited create;
  setformat;
  line:=-1;
  end;
  procedure tvczhbitmap.loadfromfile(filename:string);
  begin
  inherited loadfromfile(filename);
  setformat;
  line:=-1;
  end;
  procedure tvczhbitmap.togray;
  var x,y,r:integer;
  b:byte;
  begin
  for y:=0 to height-1 do
  for x:=0 to width-1 do
  begin
  r:=0;
  for b:=0 to 2 do
  r:=r+getbytes(x*3+b,y);
  for b:=0 to 2 do
  setbytes(x*3+b,y,r div 3);
  end;
  end;
  end.
  此后,我们需要建立几个窗体。第一个用来显示图片,第二个用来处理图片,其他的窗体都继承自第二个窗体,包含实际的处理方法。
  先看第二个窗口:
  unit untproc;
  interface
  uses
  windows, messages, sysutils, variants, classes, graphics, controls, forms,
  dialogs, extctrls, untbitmapproc, stdctrls, comctrls;
  type
  tfrmprocessor = class(tform)
  pbbar: tpaintbox;
  gpproc: tgroupbox;
  button1: tbutton;
  procedure formcreate(sender: tobject);
  procedure formdestroy(sender: tobject);
  procedure formshow(sender: tobject);
  procedure pbbarpaint(sender: tobject);
  procedure button1click(sender: tobject);
  private
  { private declarations }
  public
  { public declarations }
  bardata:array[0..255]of byte;
  bar:tvczhbitmap;
  procedure drawbar;
  end;
  var
  frmprocessor: tfrmprocessor;
  implementation
  {$r *.dfm}
  uses untviewer;
  procedure tfrmprocessor.drawbar;
  var i:integer;
  begin
  bar.canvas.fillrect(bar.canvas.cliprect);
  bar.canvas.moveto(0,255-bardata[0]);
  for i:=1 to 255 do
  bar.canvas.lineto(i,255-bardata[i]);
  end;
  procedure tfrmprocessor.formcreate(sender: tobject);
  begin
  bar:=tvczhbitmap.create;
  bar.width:=256;
  bar.height:=256;
  bar.canvas.brush.color:=clwhite;
  bar.canvas.brush.style:=bssolid;
  end;
  procedure tfrmprocessor.formdestroy(sender: tobject);
  begin
  bar.free;
  end;
  procedure tfrmprocessor.formshow(sender: tobject);
  var i:integer;
  begin
  for i:=0 to 255 do
  bardata[i]:=i;
  drawbar;
  end;
  procedure tfrmprocessor.pbbarpaint(sender: tobject);
  begin
  pbbar.canvas.draw(0,0,bar);
  end;
  procedure tfrmprocessor.button1click(sender: tobject);
  var x,y:integer;
  begin
  for y:=0 to buffer.height-1 do
  for x:=0 to buffer.width*3-1 do
  played.bytes[x,y]:=bardata[buffer.bytes[x,y]];
  frmviewer.formpaint(frmviewer);
  end;
  end.
  之后,做一个窗口继承自它,则调整bardata[]后,按apply即可看到结果。
  现在开始将图像处理。具体效果见示例程序。
  
  一、颜色反转。
  灰度图像的颜色都是从0~255,所以,为了使颜色反转,我们可以用255减去该颜色值以得到反转后的颜色。
  var i:integer;
  begin
  inherited;
  for i:=0 to 255 do
  bardata[i]:=255-i;//用255减去该颜色值
  drawbar;
  pbbarpaint(pbbar);
  end;
  
  二、缩小颜色范围以增强或减弱亮度
  颜色本来是从0~255的。如果调节它的范围,例如从0~16,则会是图像明显变暗。我们可以把起始值设为a,把终止值设为b,则新的颜色值new=a+(b-1)*old/255。这样做的话可以改变亮度,并且不会破坏原先颜色的顺序。代码如下
  var i:integer;
  begin
  for i:=0 to 255 do
  bardata[i]:=(255-sbmin.position)+round((sbmin.position-sbmax.position)/255*i);
  drawbar;
  pbbarpaint(pbbar);
  button1click(button1);
  end;
  这里的sbmin.position和sbmaxposition都是反转过的。所以使用时要用255去减
  
  三、增加某个范围内的颜色范围
  如果图像本身的颜色范围很小的画,你可以通过这种方法来加大图像的对比度,有利于对图像的分析。具体做法:
  选取一个值a做为起始值,选取一个值b做为终止值,然后按以下公式变形:
  | 0 (x<=a)
  f(x)= | 255/(b-a)*(x-a)
  | 255(x>=b)
  var i:integer;
  begin
  for i:=0 to 255 do
  begin
  if i<=sbmin.position then
  bardata[i]:=0
  else if i>=sbmax.position then
  bardata[i]:=255
  else
  bardata[i]:=round(255/(sbmax.position-sbmin.position)*(i-sbmin.position));
  end;
  drawbar;
  pbbarpaint(pbbar);
  button1click(button1);
  end;
  
  四、变为黑白图片
  在使用第三个功能的时候,你会发现当b<=a时,图像上的颜色除了黑色就是白色。这样操作的好处是不能直接显示出来的。这只要到了比较高级的图像处理如边缘检测等,才有作用。本例可以拿第三种方法的公式再变形,因此不作详细阐述。
  
  五、指数级亮度调整
  
 
  我们假设这个图的定义域是[0,1],值域也是[0,1]。那么,定义函数f(x)=x^c,则f(x)的图像有一段如上图。我们再用鼠标操作时,可以在上面取一点p(a,b),然后使f(x)通过点p,则c=ln(b)/ln(a)。有了c之后,我们就可以对颜色进行操作了:
  new=(old/255)^c*255=exp(ln(old/255)*c)*255
  var ea,eb,ec:extended;
  i:integer;
  begin
  ea:=a/255;
  eb:=b/255;
  ec:=ln(eb)/ln(ea);
  for i:=1 to 255 do
  bardata[i]:=round(exp(ln((i/255))*ec)*255);
  drawbar;
  pbbarpaint(pbbar);
  button1click(button1);
  end;
  这样做可以调节图像的亮度。
delphi图形显示特效的技巧
 概述
  ----目前在许多学习软件、游戏光盘中,经常会看到各种
  图形显示技巧,凭着图形的移动、交错、雨滴状、百页窗、积木堆叠等显现方式,使画面变得更为生动活泼,更 能吸引观众。本文将探讨如何在delphi中实现各种图形显示技巧。
  基本原理
  ----在delphi中,实现一副图象的显示是非常简单的,只要在form中定义一个timage组件,设置其picture属性,然后选 择任何有效的.ico、.bmp、.emf或.wmf文件,进行load,所选文 件就显示在timage组件中了。但这只是直接将图形显示在窗体中,毫无技巧可言。为了使图形显示具有别具一格的效果,可以按下列步骤实现:
  ----定义一个timage组件,把要显示的图形先装入到timage组件中,也就是说,把图形内容从磁盘载入内存中, 做为图形缓存。
  ----创建一新的位图对象,其尺寸跟timage组件中的图形一样。
  ----利用画布(canvas)的copyrect功能(将一个画布的矩形区域拷贝到另一个画布的矩形区域),使用技巧,动态形
  成位图文件内容,然后在窗体中显示位图。
  ----实现方法
  下面介绍各种图形显示技巧:
1.推拉效果
  将要显示的图形由上、下、左、右方向拉进屏幕内显示,同时将屏幕上原来的旧图盖掉,此种效果可分为四
  种,上拉、下拉、左拉、右拉,但原理都差不多,以上拉 效果为例。
原理:首先将放在暂存图形的第一条水平线,搬移至要显示的位图的最后一条,接着再将暂存图形的前两条水平线,依序搬移至要显示位图的最后两条水平线,然后搬移前三条、前四条叄?直到全部图形数据搬完为止。在搬移的过程中即可看到显示的位图由下而上浮起,而达到上拉的效果。
程序算法:
procedure tform1.button1click(sender: tobject);
var
newbmp: tbitmap;
i,bmpheight,bmpwidth:integer;
begin
newbmp:= tbitmap.create;
newbmp.width:=image1.width;
newbmp.height:=image1.height;
bmpheight:=image1.height;
bmpwidth:=image1.width;
for i:=0 to bmpheight do
begin
newbmp.canvas.copyrect(rect
(0,bmpheight-i,bmpwidth,bmpheight),
image1.canvas,
rect(0,0,bmpwidth,i));
form1.canvas.draw(120,100,newbmp);
end;
newbmp.free;
end;
2.垂直交错效果
原理:将要显示的图形拆成两部分,奇数条扫描线由上往下搬移,偶数条扫描线的部分则由下往上搬移,而且两者同时进行。从屏幕上便可看到分别由上下两端出现的较淡图形向屏幕中央移动,直到完全清楚为止。
程序算法:
procedure tform1.button4click(sender: tobject);
var
newbmp:tbitmap;
i,j,bmpheight,bmpwidth:integer;
begin
newbmp:= tbitmap.create;
newbmp.width:=image1.width;
newbmp.height:=image1.height;
bmpheight:=image1.height;
bmpwidth:=image1.width;
i:=0;
while i< =bmpheight do
begin
j:=i;
while j >0 do
begin
newbmp.canvas.copyrect(rect(0,j-1,bmpwidth,j),
image1.canvas,
rect(0,bmpheight-i+j-1,bmpwidth,bmpheight-i+j));
newbmp.canvas.copyrect(rect
(0,bmpheight-j,bmpwidth,bmpheight-j+1),
image1.canvas,
rect(0,i-j,bmpwidth,i-j+1));
j:=j-2;
end;
form1.canvas.draw(120,100,newbmp);
i:=i+2;
end;
newbmp.free;
end;
3.水平交错效果
原理:同垂直交错效果原理一样,只是将分成两组后的图形分别由左右两端移进屏幕。
程序算法:
procedure tform1.button5click(sender: tobject);
var
newbmp:tbitmap;
i,j,bmpheight,bmpwidth:integer;
begin
newbmp:= tbitmap.create;
newbmp.width:=image1.width;
newbmp.height:=image1.height;
bmpheight:=image1.height;
bmpwidth:=image1.width;
i:=0;
while i< =bmpwidth do
begin
j:=i;
while j >0 do
begin
newbmp.canvas.copyrect(rect(j-1,0,j,bmpheight),
image1.canvas,
rect(bmpwidth-i+j-1,0,bmpwidth-i+j,bmpheight));
newbmp.canvas.copyrect(rect
(bmpwidth-j,0,bmpwidth-j+1,bmpheight),
image1.canvas,
rect(i-j,0,i-j+1,bmpheight));
j:=j-2;
end;
form1.canvas.draw(120,100,newbmp);
i:=i+2;
end;
newbmp.free;
end;
4.雨滴效果
原理:将暂存图形的最后一条扫描线,依序搬移到可视位图的第一条到最后一条扫描线,让此条扫描线在屏幕上留下它的轨迹。接着再把暂存图形的倒数第二条扫描线,依序搬移到可视位图的第一条到倒数第二条扫描线。其余的扫描线依此类推。
程序算法:
procedure tform1.button3click(sender: tobject);
var
newbmp:tbitmap;
i,j,bmpheight,bmpwidth:integer;
begin
newbmp:= tbitmap.create;
newbmp.width:=image1.width;
newbmp.height:=image1.height;
bmpheight:=image1.height;
bmpwidth:=image1.width;
for i:=bmpheight downto 1 do
for j:=1 to i do
begin
newbmp.canvas.copyrect(rect(0,j-1,bmpwidth,j),
image1.canvas,
rect(0,i-1,bmpwidth,i));
form1.canvas.draw(120,100,newbmp);
end;
newbmp.free;
end;
5.百叶窗效果
原理:将放在暂存图形的数据分成若干组,然后依次从第一组到最后一组搬移,第一次每组各搬移第一条扫描线到可视位图的相应位置,第二次搬移第二条扫描线,接着搬移第三条、第四条扫描线.
程序算法:
procedure tform1.button6click(sender: tobject);
var
newbmp:tbitmap;
i,j,bmpheight,bmpwidth:integer;
xgroup,xcount:integer;
begin
newbmp:= tbitmap.create;
newbmp.width:=image1.width;
newbmp.height:=image1.height;
bmpheight:=image1.height;
bmpwidth:=image1.width;
xgroup:=16;
xcount:=bmpheight div xgroup;
for i:=0 to xcount do
for j:=0 to xgroup do
begin
newbmp.canvas.copyrect(rect
(0,xcount*j+i-1,bmpwidth,xcount*j+i),
image1.canvas,
rect(0,xcount*j+i-1,bmpwidth,xcount*j+i));
form1.canvas.draw(120,100,newbmp);
end;
newbmp.free;
end;
6.积木效果
原理:是雨滴效果的一种变化,不同之处在于,积木效果每次搬移的是一块图形,而不只是一根扫描线。
程序算法:
procedure tform1.button7click(sender: tobject);
var
newbmp:tbitmap;
i,j,bmpheight,bmpwidth:integer;
begin
newbmp:= tbitmap.create;
newbmp.width:=image1.width;
newbmp.height:=image1.height;
bmpheight:=image1.height;
bmpwidth:=image1.width;
i:=bmpheight;
while i>0 do
begin
for j:=10 to i do
begin
newbmp.canvas.copyrect(rect(0,j-10,bmpwidth,j),
image1.canvas,
rect(0,i-10,bmpwidth,i));
form1.canvas.draw(120,100,newbmp);
end;
i:=i-10;
end;
newbmp.free;
end;
结束语
上述图形显示效果均已上机通过。使用效果很好。
用delphi实现图像放大镜
  向窗体上添加两个timage组件,其中一个timage组件的name属性设置为image1,它充当原图片显示的载体。另一个timage组件的name属性设置为image2,它可以显示放大后的图像。添加组件后的窗体如图1所示。
 
图1 添加组件后的窗体
  本例的核心是stretchblt函数,利用stretchblt函数实现局部图像放大,响应代码如下:
procedure tform1.image1mousemove(sender: tobject; shift: tshiftstate; x, y: integer);
begin
 stretchblt(image2.canvas.handle,0,0,image2.width,image2.height,
 image1.canvas.handle, x-20,y-20,40,40,srccopy);
 image2.refresh;
 screen.cursors[1]:=loadcursorfromfile(’magnify.cur’);
 self.cursor:=1;
end;
  程序首先会调用stretchblt函数,以鼠标当前位置作为中心点,以边长为40选中image1组件上的局部图像,并放大此局部图像到image2组件上。然后通过调用image2组件的refresh方法以刷新image2组件的显示。最后设置鼠标指针为新的形状。
  程序代码如下:
unit unit1;
interface
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, extctrls, stdctrls;
type
 tform1 = class(tform)
 image1: timage;
 image2: timage;
 procedure image1mousemove(sender: tobject; shift: tshiftstate; x,y: integer);
 procedure formmousemove(sender: tobject; shift: tshiftstate; x,y: integer);
private
 { private declarations }
public
 { public declarations }
end;
var
 form1: tform1;
 implementation
 {$r *.dfm}
 procedure tform1.image1mousemove(sender:tobject;shift:tshiftstate;x,y: integer);
 begin
  stretchblt(image2.canvas.handle,0,0,image2.width,image2.height,image1.canvas.handle, x-20,y-20,40,40,srccopy);
  image2.refresh;
  screen.cursors[1]:=loadcursorfromfile(’magnify.cur’);
  self.cursor:=1;
end;
procedure tform1.formmousemove(sender: tobject; shift: tshiftstate; x,y: integer);
begin
 screen.cursors[1]:=crdefault;
 self.cursor:=1;
end;
end.
  

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Delphi基本图像处理代码
探究Delphi的图形处理(二)
(1)delphi,用delphi制作类似按键精灵的功能
delphi WebBrowser控件上网页验证码图片识别教程(一)
一个实用的Delphi屏幕截图程序的设计
delphi - 如何打印 TPanel 内容?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服