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 6242815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:01:30+00:00 2026-05-24T12:01:30+00:00

I have an HTML5 canvas element which I would like to save to the

  • 0

I have an HTML5 canvas element which I would like to save to the server as a monochrome bmp. I have seen examples online of saving it as a color bmp, but because of size and colorspace restrictions my application has I need it to be a small monochrome bmp. Does anyone know of a simple solution for this (or am I going to have to write a class to create a bmp file from scratch?)

  • 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-05-24T12:01:32+00:00Added an answer on May 24, 2026 at 12:01 pm

    I found a good solution by working off of this example code: rephrase.net/box/bitmap The author helped me implement a function that could turn a pixel array into a monochrome bitmap. From there I was able to take canvas data and iterate through it and parse it into a pixel array that I could convert into a monochrome bmp.

    I created this function to facilitate converting canvas data based on alpha values (since the canvas data is also monochrome in my application) to a pixel array which can be used by the jsbmp script:

    function createBMP(canvas) {
        var context = canvas.getContext("2d");
        var width = canvas.width;
        var height = canvas.height;
        var imageData = context.getImageData(0, 0, width, height);
        var data = imageData.data;
    
        //create pixel array from canvas based on alpha
        var pixels = [];
        for (var i = data.length - 1; i > 0; i -= 4) {
            if (i > 0) {
                if (data[i] > 125) {
                    pixels.push("000000");
                } else {
                    pixels.push("ffffff");
                }
            }
        }
    
        //unmirror
        var pixarray = [];
        for (var i = height - 1; i > -1; i--) {
            var row = [];
            for (var j = 0; j < width; j++) {
                row.push(pixels[(i * width) + j]);
            }
            for (var j in row) {
                pixarray.push(row[j]);
            }
        }
        pixarray.reverse();
    
        return bmp_mono(width, height, pixarray);
    }
    

    And here is the code for bmp_mono:

    /*
    Create an uncompressed Windows bitmap (monochrome) given width, height and an
    array of pixels.
    
    Pixels should be in BMP order, i.e. starting at the bottom left, going up
    one row at a time.
    */
    function bmp_mono(width, height, pixarray, palette) {
    var rowsize = Math.ceil(width / 8);
    var rowpadding = 4 - (rowsize % 4);
    if (typeof palette == 'undefined')
        palette = ['000000', 'ffffff'];
    
    var j, pix, mod;
    pixarray.reverse();
    var pixels = [];
    var b = 0;
    for (var i=0; i<height; ++i) {
        row = [];
        for (j=0; j<width; ++j) {
            mod = j % 8;
            pix = pixarray.pop();
            if (parseInt(pix, 16) != 0) {
                b = b | Math.pow(2, 7-mod);
            }
            if (mod == 7 || j == width-1) {
                pixels.push(String.fromCharCode(b));
                b = 0;
            }
        }
        for (j=0; j<rowpadding; ++j) {
            pixels.push("\x00");
        }
    }
    return _bmp(width, height, palette, pixels.join(""), 1, 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a snake game using javascript and HTML5 Canvas element. It can
I have HTML code edited by FCKEditor stored in a database and would like
The HTML5 <canvas> element does not accept relative sizes (percent) for its width and
I have html code that looks roughly like this: <div id=id1> <div id=id2> <p>some
I have HTML code like this : <div> <a>Link A1</a> <a>Link A2</a> <a>Link A3</a>
I have an HTML page (say welcome.html) which contains an iframe to a page
I have some HTML that looks like this: <ul class=faq> <li class=open> <a class=question
I'm developing a canvas graphical editor. I have already coded an undo-redo system that
Assuming that I have a WebGL canvas (by calling getContext("experimental-webgl") ). Is there any
I have a div element that I'm trying, basically, to move wherever the user

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.