Autor Beitrag
Tendl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 117



BeitragVerfasst: Mo 03.03.03 09:22 
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.

ausblenden volle Höhe Quelltext
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 user profile iconKlabautermann: Code-Tags hizugefügt.
Rool
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 211



BeitragVerfasst: Mo 03.03.03 13:08 
Titel: ...
Du musst die unit "jpeg" einbinden, dann dein bild konvertieren und dann kannst du alles so machen wie bisher.

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
function JpegToBmp(JpegFileName: string): TBitmap
  var jpeg: TJPEGImage;
  bmp: TBitmap;
begin
  jpeg := TJPEGImage.Create;
  try
    jpeg.LoadFromFile(JpegFileName);
    bmp := TBitmap.Create;
    try
      bmp.Assign(jpeg);
      result.Assign(bmp);
    finally
      bmp.free;
    end;
  finally
    jpeg.free;
  end;
end;

_________________
MFG Rool
Tendl Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 117



BeitragVerfasst: Di 04.03.03 09:44 
Titel: Danke schön, hat gepasst
Die JPEG-Unit hat ich zuerst schon eingebunden, aber bei den Assigns der Bitmaps hat ich wohl ein Problem.