Hallo,
ich hab folgendes Problem: Ich stelle ein Bild auf einem Panel dar. Die Farbe des Panels sollte sich dem Bildrand einigermaßen anpassen. Für BMP-Bilder funktioniert das ganze auch wie erwartet. Bei JPGs klappte es nicht, offensichtlich muß ich da anderes auf die Eigenschaften Height und Width, sowie auf die einzelnen Pixel zugreifen.
Wer kann helfen?
Danke
Thomas
Anbei das komplette Unit, vielleicht kann es ja sonst noch jemand brauchen.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69:
| unit suggest; interface uses Graphics,ExtCtrls; function suggest_color(Image: TImage): TColor;
implementation function suggest_color(Image: TImage): TColor; var i, j, n: integer; summe: array[1..3] of integer; color: array[1..3] of byte; width, height: integer;
procedure add_point(p: TColor); {splits TColor to RGB and sums the points} begin if p = -1 then begin n := n - 1; end else begin color[1] := (p and $FF0000) shr 16; color[2] := (p and $00FF00) shr 8; color[3] := (p and $FF);
summe[1] := summe[1] + color[1]; summe[2] := summe[2] + color[2]; summe[3] := summe[3] + color[3]; end; end;
begin {Initialize sums} summe[1] := 0; summe[2] := 0; summe[3] := 0; {calculate number of points} width := Image.Picture.Width; height := Image.Picture.Height; n := 2 * Width; n := n + 2 * height; {Loop over horizontal borders} for I := 0 to Width - 1 do begin add_point(Image.Picture.Bitmap.Canvas.Pixels[i, 0]); add_point(Image.Picture.Bitmap.Canvas.Pixels[i, Image.Height - 1]); end; {Loop over vertical borders} for I := 1 to height - 1 do begin add_point(Image.Picture.Bitmap.Canvas.Pixels[0, i]); add_point(Image.Picture.Bitmap.Canvas.Pixels[Image.width - 1, i]); end; {calculate average for each color} for I := 1 to 3 do begin summe[i] := round(summe[i] / n); end; {Back to TColor} summe[1] := (integer(summe[1] shl 16)) + (integer(summe[2] shl 8)) + integer(summe[3]); result := summe[1]; end;
end. |
Moderiert von
Klabautermann: Code-Tags hizugefügt.