AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Multimedia Delphi Wie Bitmap per API erstellen und speichern
Thema durchsuchen
Ansicht
Themen-Optionen

Wie Bitmap per API erstellen und speichern

Offene Frage von "Zacherl"
Ein Thema von Zacherl · begonnen am 17. Jun 2010 · letzter Beitrag vom 18. Jun 2010
Antwort Antwort
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.666 Beiträge
 
Delphi 12 Athens
 
#1

AW: Wie Bitmap per API erstellen und speichern

  Alt 18. Jun 2010, 09:31
Evtl. ist der MSDN-Artikel zu MSDN-Library durchsuchenCreateBitmap hilfreich. Im ersten Link ist auch ein Beispiel in C++ (das scheint zwar für Windows CE zu sein, macht aber nichts).
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
blackfin
(Gast)

n/a Beiträge
 
#2

AW: Wie Bitmap per API erstellen und speichern

  Alt 18. Jun 2010, 10:27
Das hier könnte dir vielleicht auch helfen:

http://www.winprog.org/tutorial/bitmaps.html
  Mit Zitat antworten Zitat
Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#3

AW: Wie Bitmap per API erstellen und speichern

  Alt 18. Jun 2010, 12:41
Danke ich habe es nun hinbekommen ein Bitmap zu erstellen und zu speichern. Ein Problem gibt es allerdings noch, wenn ich bei der Farbtiefe auf unter 16 Bits gehe. Dann kann das Bild gespeicherte Bild nicht dargstellt werden und im Hexeditor sieht das Farbarray auch komplett anders aus.

Delphi-Quellcode:
function PixelFormatToBitCount(Format: TPixelFormat): Integer;
begin
  Result := 0;
  case Format of
    pf1bit: Result := 1;
    pf4bit: Result := 4;
    pf8bit: Result := 8;
    pf15bit: Result := 15;
    pf16bit: Result := 16;
    pf24bit: Result := 24;
    pf32bit: Result := 32;
  end;
end;

procedure CreateBitmap(var BitmapDC: HDC; var Bitmap: HBITMAP;
  var BitmapMemory: Pointer; Width, Height: Integer; Format: TPixelFormat);
var
  BMI: BITMAPINFO;
begin
  BitmapDC := CreateCompatibleDC(0);
  with BMI do
  begin
    bmiHeader.biSize := SizeOF(bmi.bmiHeader);
    bmiHeader.biWidth := Width;
    bmiHeader.biHeight := Height;
    bmiHeader.biPlanes := 1;
    bmiHeader.biBitCount := PixelFormatToBitCount(Format);
    bmiHeader.biCompression := BI_RGB;
    bmiHeader.biSizeImage := 0;
    bmiHeader.biXPelsPerMeter := 0;
    bmiHeader.biYPelsPerMeter := 0;
    bmiHeader.biClrUsed := 0;
    bmiHeader.biClrImportant := 0;
  end;
  Bitmap := CreateDIBSection(BitmapDC, BMI, DIB_RGB_COLORS, BitmapMemory, 0, 0);
  SelectObject(BitmapDC, Bitmap);
end;

procedure DeleteBitmap(BitmapDC: HDC; Bitmap: HBITMAP);
begin
  DeleteDC(BitmapDC);
  DeleteObject(Bitmap);
end;

function CalculateImageSize(BitmapInfoX: BITMAPINFOHEADER): Cardinal;
begin
  Result := Round((BitmapInfoX.biBitCount / 8) * BitmapInfoX.biWidth *
    BitmapInfoX.biHeight);
end;

procedure CreateBitmapScreenShot(Format: TPixelFormat; Ratio: TResolutionRatio);
var
  C: HDC;
  NormalWidth, NormalHeight,
  RatioWidth, RatioHeight: Integer;
  BitmapDC: HDC;
  Bitmap: HBITMAP;
  BitmapMemory: Pointer;
  BitmapInfoX: BITMAPINFOHEADER;
  BitmapHeader: BITMAPFILEHEADER;
  hFile: THandle;
  dwWritten: DWord;
begin
  C := GetWindowDC(GetDesktopWindow);
  try
    NormalWidth := GetSystemMetrics(SM_CXSCREEN);
    NormalHeight := GetSystemMetrics(SM_CYSCREEN);
    RatioWidth := Round(NormalWidth * 0.01 * Ratio);
    RatioHeight := Round(NormalHeight * 0.01 * Ratio);

    CreateBitmap(C, BitmapDC, Bitmap, BitmapMemory, RatioWidth, RatioHeight, Format);
    if (Bitmap <> 0) then
    try
      StretchBlt(BitmapDC, 0, 0, RatioWidth, RatioHeight, C, 0, 0, NormalWidth,
        NormalHeight, SRCCOPY);

      FillChar(BitmapInfoX, SizeOf(BITMAPINFOHEADER), #0);
      BitmapInfoX.biSize := SizeOf(BITMAPINFOHEADER);
      BitmapInfoX.biWidth := RatioWidth;
      BitmapInfoX.biHeight := RatioHeight;
      BitmapInfoX.biPlanes := 1;
      BitmapInfoX.biBitCount := PixelFormatToBitCount(Format);
      BitmapInfoX.biCompression := BI_RGB;
      BitmapInfoX.biSizeImage := CalculateImageSize(BitmapInfoX);
      FillChar(BitmapHeader, SizeOf(BITMAPFILEHEADER), #0);
      BitmapHeader.bfType := $4D42;
      BitmapHeader.bfOffBits := SizeOf(BITMAPINFOHEADER) + SizeOf(BITMAPFILEHEADER);
      BitmapHeader.bfSize := BitmapHeader.bfOffBits + BitmapInfoX.biSizeImage;
      hFile := CreateFile('C:\test.bmp', GENERIC_WRITE, FILE_SHARE_READ, nil,
        CREATE_ALWAYS, 0, 0);
      WriteFile(hFile, BitmapHeader, SizeOf(BITMAPFILEHEADER), dwWritten, nil);
      WriteFile(hFile, BitmapInfoX, SizeOf(BITMAPINFOHEADER), dwWritten, nil);
      WriteFile(hFile, BitmapMemory^, BitmapInfoX.biSizeImage, dwWritten, nil);
      CloseHandle(hFile);
    finally
      DeleteBitmap(BitmapDC, Bitmap);
    end;
  finally
    ReleaseDC(0, C);
  end;
end;
Muss ich für Farbtiefen unter 16 bit eventuell etwas an der Farbpalette anpassen oder so?

Viele Grüße
Zacherl
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:11 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz