Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8194711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:54:54+00:00 2026-06-07T04:54:54+00:00

How to draw pixels to a TImage but in pf1bit bitmap format? I have

  • 0

How to draw pixels to a TImage but in pf1bit bitmap format? I have tried but the result is the whole of the image are black.

here is the code I’ve tried :

image1.picture.bitmap.loadfromfile('example.bmp'); // an image which is RGB pf24bit with 320 x 240 px resolution
image1.picture.bitmap.pixelformat := pf1bit;
for i:=0 to round(image1.picture.bitmap.canvas.height/2) - 1 do
  begin
    for j:=0 to round(image1.picture.bitmap.canvas.width/2) - 1 do
      begin
        image1.picture.bitmap.Canvas.pixels[i,j]:=1; // is this correct? ... := 1? I've tried to set it to 255 (mean white), but still get black 
      end;
  end;

note that the image size is 320×240 pixel.

Thanks before.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:54:56+00:00Added an answer on June 7, 2026 at 4:54 am

    You need to pack 8 pixels into a single byte for 1 bit color format. The inner loop would look like this:

    var
      bm: TBitmap;
      i, j: Integer;
      dest: ^Byte;
      b: Byte;
      bitsSet: Integer;
    begin
      bm := TBitmap.Create;
      Try
        bm.PixelFormat := pf1bit;
        bm.SetSize(63, 30);
        for i := 0 to bm.Height-1 do begin
          b := 0;
          bitsSet := 0;
          dest := bm.Scanline[i];
          for j := 0 to bm.Width-1 do begin
            b := b shl 1;
            if odd(i+j) then
              b := b or 1;
            inc(bitsSet);
            if bitsSet=8 then begin
              dest^ := b;
              inc(dest);
              b := 0;
              bitsSet := 0;
            end;
          end;
          if b<>0 then
            dest^ := b shl (8-bitsSet);
        end;
        bm.SaveToFile('c:\desktop\out.bmp');
      Finally
        bm.Free;
      End;
    end;
    

    The output looks like this:

    enter image description here

    Update

    Rob’s comment prompted me to look at using Pixels[] rather than the bit-twiddling above. And indeed it is perfectly possible.

    var
      bm: TBitmap;
      i, j: Integer;
      Color: TColor;
    begin
      bm := TBitmap.Create;
      Try
        bm.PixelFormat := pf1bit;
        bm.SetSize(63, 30);
        for i := 0 to bm.Height-1 do begin
          for j := 0 to bm.Width-1 do begin
            if odd(i+j) then begin
              Color := clBlack;
            end else begin
              Color := clWhite;
            end;
            bm.Canvas.Pixels[j,i] := Color;
          end;
        end;
        bm.SaveToFile('c:\desktop\out.bmp');
      Finally
        bm.Free;
      End;
    end;
    

    Since each call to assign Pixels[] results in a call to the Windows API function SetPixel, the bit-twiddling code would perform better. Of course, that would only ever matter if your bitmap creation code was a performance hot-spot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to draw a line of 2 pixels..I have written the following code
I'd like to draw antialiased text on a transparent bitmap and have the antialiasing
I want to draw a bitmap, but with only gray pixel (for example 0x555555
I am writing a simple little piece of code to draw pixels wherever the
I want to draw five rectangle bars in Android. I have the regtangles, but
I am trying to draw individual pixels in xcode. I already know Objective-C but
I have to draw several thousands of pixels per frame for a falling sand
I have some simple ImageSource. I want to 'draw' some string every 20 pixels
Requirement: To draw one Bitmap Image and rectangle(s) based on the collection of points.
Hey just wondering how I can draw multiple rectangle objects in C# but have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.