Easiest Way to Generate a Bitmap

Warning! Some information on this page is older than 3 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

15:35
Wed
19
Oct 2011

Easiest Way to Generate a Bitmap

I wanted to visualize results of some mathematical computations so I wondered what is the easiest way to generate and then show or save a bitmap? I considered Scilab, PHP with GD library, C++ with WinAPI GDI, C++ with some library that have image file formats support like FreeImage... Finally I've came up with the following solution, using only C++ and WinAPI to fill appropriate data structures and save the image as 24-bit BMP file.

// Width and height of the bitmap
unsigned size_x = 1280, size_y = 720;
// Rows top-down,
// for each row pixels from left to right,
// for each pixel components B, G, R = 0..255.
unsigned data_size = size_x * size_y * 3;
unsigned char *data = new unsigned char[data_size];

// Here fill the data, for example:
for (unsigned y = 0, i = 0; y < size_y; ++y) {
  for (unsigned x = 0; x < size_x; ++x) {
    data[i++] = 255; // G
    data[i++] = 255; // B
    data[i++] = 255; // R
  }
}

// Prepare data structures
DWORD data_offset = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
DWORD file_size = data_offset + data_size;
BITMAPFILEHEADER file_header = {
  0x4D42, // bfType = "BM"
  file_size, // bfSize
  0, 0, // bfReserved1, bfReserved2
  data_offset, // bfOffBits
};
BITMAPINFOHEADER info_header = {
  sizeof(BITMAPINFOHEADER), // biSize
  (LONG)size_x, -(LONG)size_y, // biWidth, biHeight
  1, // biPlanes
  24, // biBitCount
  BI_RGB, // biCompression
  0, // biSizeImage
  72, 72, // biXPelsPerMeter, biYPelsPerMeter
  0, 0, // biClrUsed, biClrImportant
};

// Save file
FILE *file = fopen("file_name.bmp", "wb");
fwrite(&file_header, sizeof(file_header), 1, file);
fwrite(&info_header, sizeof(info_header), 1, file);
fwrite(&data[0], 1, data_size, file);
fclose(file);

// Finalize
delete [] data;

Comments (1) | Tags: rendering winapi c++ | Author: Adam Sawicki | Share

Comments

yll
2016-03-25 03:20:28
If http://www.guccishoes-uk.co.uk/ you http://www.bottega-veneta.us.com/ are http://www.eyeglassframes.in.net/ happy http://www.oakleys-sunglasses.us.com/ temperament, http://www.truereligion-outlet.com.co/ I http://www.coachoutlet-online.com.co/ have http://www.nikeshoesoutlet.org.uk/ a song, dance with your http://www.tiffanyandco.net.co/ favorite http://www.instylers.us.com/ look; http://www.oakleys-glasses.us.com/ if http://www.louis-vuitton-australia.com.au/ you http://www.bcbg-dresses.com/ rife, I http://www.converse-shoes.net/ will become http://www.ferragamo.com.co/ a http://www.swarovskicanada.ca/ cloud, falling http://www.pandorajewelry.top/ some http://www.coachoutletstore.net.co/ peerless http://www.tommy-hilfiger-canada.ca/ situation; http://www.burberry-outlet.net.co/ for this flower http://www.abercrombie-and-fitch.us.com/ Dimei http://www.air-max.com.de/ under http://www.ralph-lauren.com.au/ Xu, http://www.omega-watches.us.com/ Xu http://www.weddingdressesuk.org.uk/ next stop for http://www.jordanretro.org/ this http://www.newoutletonlinemall.com/ cloud, http://www.katespadeoutlet.gb.net/ let http://www.swarovski--uk.me.uk/ that http://www.uggboots.com.de/ drop of http://www.abercrombie-kids.us.com/ rain http://www.michael-kors.com.co/ touched http://www.uggboots.net.co/ that http://www.michael-kors-outlet-online.us.org/ masterpiece http://www.cheap-baseballbats.net/ encounter; http://www.oakley-sunglasses.mex.com/ not http://www.cheapjerseys.com.co/ to http://www.prada-shoes.com.co/ ask, in http://www.nikefactory.org/ the http://www.kate-spade.gb.net/ southern http://www.salvatoreferragamo.in.net/ misty http://michaelkors.euro-us.net/ rain ink, http://www.p90xworkout.us.com/ or http://www.oakley.org.es/ vegetation desolate http://www.nike-schoenen.co.nl/ Saibei http://www.christianlouboutinshoesoutlet.org/ only hope http://www.jordanrelease-dates.us.com/ that http://www.adidas--canada.ca/ you read http://www.kate-spade.in.net/ in http://www.adidas-superstar.de/ conjunction rain http://www.louisvuitton.jp.net/ Partly http://www.abercrombie-fitchs.us.com/ farming, http://www.insanity-workout.us.com/ leisure together http://www.hollister-abercrombie.com.se/ Immersió http://www.replica-handbags.in.net/ listen to http://www.rolex-watches.us.com/ the http://www.juicycouture.com.co/ birds, http://www.cheapuggboots.us.com/ over the http://www.barbour-jacketsoutlet.com/ days http://www.rayban-sunglasses.us.org/ together with http://www.guccishoes.com.co/ ordinary http://www.christian--louboutin.in.net/ water http://www.ugg-australia.com.de/ .

If grinding moonlight, the wind http://www.wedding--dresses.ca/ pen, next month before it flowers, http://www.beatsbydre.com.co/ pen http://www.poloralphlaurenoutlet.net.co/ years http://www.ralphlaurenoutletonline.in.net/ water http://www.harrods-london.co.uk/ volume; http://www.canada-goosesjackets.us.com/ with http://www.uggsaustralia.com.co/ traces breeze http://www.mcmsworldwide.com/ atmosphere, clouds, http://www.converse.net.co/ and http://www.jimmy-chooshoes.com/ in a paper http://coach.euro-us.net/ by http://www.coach-factory.in.net/ the http://www.tory-burchshoesoutlet.net/ recollection http://www.nikestore.com.de/ of old http://www.giuseppezanotti.com.co/ books you http://www.soft-ballbats.com/ warm; http://www.lululemonoutlet.gb.net/ make http://www.longchamp.us.org/ clear http://www.hermesbirkin-bag.net/ water http://www.moncler-outlet.us.org/ in http://www.ugg-bootsclearance.com/ Qiannian landscape http://www.hollisteronlineshop.com.de/ outside, http://www.tomsoutletonline.net/ waiting http://www.timberlandshoes.net.co/ for http://www.cheap-raybans.com/ the http://www.toms-outlets.us.com/ moon and new http://www.tommy-hilfiger.co.nl/ moon; http://www.maccosmetics.net.co/ quietly http://www.coach-outletonline.net.co/ spend http://www.louisvuitton.so/ the joy http://www.michael-kors.com.es/ of http://www.newbalance.com.es/ silence, do http://www.nike-shoesoutlet.us.com/ not ask http://www.abercrombie-fitchsale.com/ how http://mcmbackpack.co-om.com/ changes of http://www.burberry-outlets.co.uk/ marshes, http://www.longchamp-handbags.us.com/ how http://www.michael-kors-australia.com.au/ rotary http://www.raybans.us.org/ years, http://www.tiffanyandco-canada.ca/ the http://www.reebok.com.de/ only http://www.louis-vuitton-taschen.com.de/ place http://www.pandora-charms-canada.ca/ in http://www.guccioutlet-sale.in.net/ the bridges for http://www.rayban.co.nl/ you as http://www.coachoutletstore-online.com.co/ I ended classic.

Life http://www.gucci-taschen-outlet.de/ would http://www.longchamp.com.co/ like to http://www.nike.org.es/ make http://www.michaelkorsoutlet.ar.com/ the dust http://www.abercrombieand-fitch.ca/ of http://www.outlet-burberry.net.co/ a http://www.burberryonlineshop.de/ lotus, http://www.toms--outlet.com.co/ the http://www.burberry-outletonlinesale.in.net/ mountain http://www.polooutlets-store.com/ waiting for a ride a http://airhuarache.shoesoutlet.us.com/ water http://www.maccosmetics.gr.com/ ride with http://www.pradahandbags.net.co/ a http://northfaceoutlet.co-om.com/ window http://www.hogan.com.de/ the http://www.nike-air-max.ca/ bright http://www.nike-air-max.com.de/ window, http://monsterbeats.co-om.com/ the sun http://www.adidas.com.se/ was http://www.coachblackfriday.com/ warm http://www.michaeljordan.com.de/ embrace, Wan http://www.lululemon-australia.com.au/ Wan http://www.swarovskijewelry.com.co/ in a http://www.asicsgels.de/ new Green Mile, http://www.louisvuitton-outlets.us/ this http://www.yoga-pants.ca/ life http://www.nikerosherun.us/ filled with http://www.thenorthfacejackets.net.co/ love; http://www.hermes-outlet.com.co/ make a http://www.handbagsoutlet.net.co/ colorful http://www.bebeclothing.in.net/ in http://www.mizuno-running.net/ full http://www.nike-rosherun.nl/ bloom http://www.cheap-oakleyglasses.us.com/ throughout http://www.marcjacobs.us.com/ the spring, to http://www.eyeglassesonline.us.com/ meet http://www.pulseras-pandora.com.es/ a beautiful http://www.michael-kors-outlet.us.org/ painting http://www.lululemoncanada.ca/ on http://www.coachhandbags2016.us.com/ the successful!

A http://www.michaelkorsoutlet-online.ar.com/ tea seats bed, http://www.toryburchsale.com.co/ a http://www.burberryoutlet-sale.in.net/ pillow http://www.ralph-laurenoutlet.ca/ Scrolls http://www.michael-kors-canada-outlet.ca/ fine http://www.chiflatiron.net.co/ ponder. This http://www.polo-ralph-lauren.de/ is a http://www.polo-outlets.com.co/ simple http://www.louis--vuitton.org.uk/ literature, http://www.nikeskos.dk/ this http://www.nfl-jersey.us.com/ is http://www.beats-headphone.com.co/ the http://www.mcm-bags.in.net/ classic psychic, http://supra.shoesoutlet.us.com/ regardless http://www.pradaoutlet.com.co/ of gem material, http://www.michael-kors-handbags.us.com/ regardless http://www.louisvuittons.com.co/ of http://www.nike-air-force.de/ the net http://www.oakley.com.de/ worth of http://www.longchamphandbagsoutlet.net/ the poor, http://www.jordan-shoes.com.co/ because http://www.cheaprayban.com.co/ the http://www.celine-bags.org/ share http://www.rolexwatchesforsale.us.com/ of http://www.barbour-factory.com/ hazy http://www.hollister.us.org/ love, love deep into http://www.montblanc--pens.in.net/ the http://www.uggsoutlet.com.co/ soul.

This http://www.tiffany-und-co.de/ literary http://www.airhuarache.co.uk/ love, http://www.nike-roshe-run.de/ love http://www.converse.com.de/ this http://www.cheapshoes.com.co/ millennia-old http://www.rayban.com.de/ Chinese http://www.abercrombie-hollister.nl/ soul http://www.tommy-hilfiger.com.de/ of the http://www.nikefree5.net/ wind. http://www.hollister-clothing.in.net/ Or http://www.michaelkorsoutletonline-sale.us.com/ vigorous, http://www.ugg-boots.us.org/ soul-stirring, http://www.louis-vuittonblackfriday.com/ like http://www.rosheruns.us/ Longyou song marsh, Huang http://www.louis-vuittonoutletcanada.ca/ Fei nine http://www.newbalance-shoes.org/ days; http://www.burberryoutlet-canada.ca/ or http://www.longchamp.com.de/ single http://www.nike-air-max.us/ elevation http://www.tommyhilfigeroutlet.in.net/ grid, http://www.thenorthface.com.de/ noble Aoshuang, http://www.christianlouboutinshoes.jp.net/ like http://www.chanelhandbags.net.in/ Pine Valley http://www.oakley-outletstore.in.net/ Health, http://www.christianlouboutinoutlet.net.co/ Xia Ying http://www.nike-schuhe.com.de/ Cheng Tong; http://www.horloges-rolex.nl/ or http://www.tommy-hilfigeroutlet.com/ want http://www.ralphs-lauren.co.uk/ to http://www.cheapoakley-sunglasses.in.net/ go http://www.nike-skor.com.se/ think, http://www.adidas-schuhe-online.de/ therefore, become http://www.hollisterclothingstore.org/ lazy Ke http://www.thenorth-faces.co.uk/ Zhou, http://www.burberry-handbagsoutlet.com.co/ Shen http://www.pandorajewellery.com.au/ round http://www.toryburchoutlet-sale.us.com/ like the http://www.bottegaveneta-bagsoutlet.com/ moon http://www.iphone-cases.net.co/ and http://www.christian-louboutin-shoes.ca/ stars, http://www.tnf-jackets.us.com/ the http://www.guccihandbags.net.co/ rivers into http://www.uggs-onsale.net/ the ocean; http://www.ugg-uggboots.net/ or http://www.rayban-sunglassesoutlet.co.uk/ plaintive http://www.tiffany-jewelry.net/ blurred, http://www.michaelkors-uk.org.uk/ cold http://www.rolex-watch.me.uk/ hesitation, like Su http://www.pumashoes.in.net/ Yi http://www.ralphlaurenonlineshop.de/ impermanence, cold http://michaelkors.co-om.com/ feast http://www.cheap-jordans.net/ next http://www.michael-korshandbags.org.uk/ month. http://www.mk-outlet.us.com/ Let him how Mind, http://www.cheapoakley-sunglasses.com/ only http://www.yoga-pants.net.co/ for http://www.asics-outlet.us.com/ the http://www.guccishoes.us.org/ love http://www.michaelkors.co.nl/ affair http://www.cheapmichaelkors.us.org/ Everlasting, http://www.raybans-sunglassesoutlet.in.net/ the text from http://www.ugg-boots-australia.com.au/ the heart http://www.ray--ban.ca/ Think http://www.michaelkors.com.se/ that summer, a http://www.ralphslauren-outlet.co.uk/ column http://www.longchampoutlet.com.co/ Chen Xiang a http://www.cheap-nike-shoes.net/ cool http://www.replica-watches.com.co/ pillow, http://www.rayban.org.es/ which is the http://www.lauren-ralph.co.uk/ roots of http://www.burberrysoutlet2016.com/ what http://www.mmoncler-outlet.com/ you need http://www.rolexwatches-canada.ca/ to make http://www.the-northfacejackets.us.com/ tea http://www.cheapjerseys.us.org/ cooking wine, http://www.edhardy.us.org/ Hongxiutianxiang http://www.guccishoes.in.net/ just http://www.swarovski-australia.com.au/ hand http://www.michael-kors.net.co/ poetry http://www.the-northface.net.co/ twos http://www.supra-shoes.org/ Bliss http://www.swarovski-online-shop.de/ half http://www.burberrybags-outlet.com/ a http://www.louboutin.jp.net/ day can http://www.northfaceoutlet.com.co/ tolerate http://www.babyliss-pro.us.com/ you wander. http://www.thejoreseproject.com/ Static http://www.michael-kors-taschen.com.de/ water http://www.coach-purseoutlet.net/ body, http://www.woolrich-clearance.com/ Shen http://www.louisvuitton-outlet.com.co/ soul dream, http://www.designer-handbagsoutlet.us.com/ it http://www.oakley-sunglassoutlet.net/ is http://www.vans-schuhe.com.de/ only http://www.retro-jordans.com/ a few clouds color country http://www.basketballshoes.com.co/ Pitt, rivers http://www.calvin-kleins.us.com/ and http://www.oakleysunglasses-cheap.in.net/ mountains, jade http://www.tomsshoes-outlet.us.com/ group. http://www.ghd-hairstraightener.net/ Do http://www.ralph-laurens.org.uk/ not http://www.nikefree-run.net/ ask http://www.tommyhilfiger.net.co/ bustling http://www.bcbg-max-azria.ca/ past, http://www.chi-flatiron.us.com/ gone; http://www.adidas.org.es/ do not http://www.tiffany-andco.com.au/ ask flowery http://www.the-north-face.ca/ Meijuan, http://www.levisjeans.com.co/ Homecoming, http://nike.shoesoutlet.us.com/ heart http://www.swarovski-crystal.us.com/ like http://www.ugg-boots.ca/ hanging http://www.timberlandbootsoutlet.us.com/ a mirror, according http://www.coach-factoryoutlet.net.co/ to http://www.thomas-sabos.org.uk/ Italian Bodhi http://www.soccer-shoes.us.com/ station.

Three http://www.canadagooses-2016.com/ thousand Red http://www.tocoachoutlet.com/ ink, http://www.chanel-bags.com.co/ as are http://www.beats-by-dre.com.co/ cold-blooded http://www.hermesbags.jp.net/ bustling http://www.the-northface.com.co/ paper, into a http://www.truereligionjeans.net.co/ word http://www.michaelkors.so/ discretion. Five thousand http://uggaustralia.misblackfriday.com/ years http://www.juicycoutureoutlet.net.co/ of http://www.newbalance-outlet.org/ calligraphy love http://www.nike-air-max.com.se/ this, http://www.toms-shoes.net.co/ love http://www.air-huarache.co.uk/ flames http://www.rayban-wayfarer.in.net/ smoke sand http://www.new-balance-schuhe.de/ which http://www.mkoutletonline.us.com/ contained http://www.mcmhandbags.com.co/ five thousand years. http://www.omegarelojes.es/ Regardless http://www.uggs.in.net/ of the time, and http://www.nike-roshe-run.com.es/ regardless http://www.tommy-hilfiger-online-shop.de/ of http://www.coach-factoryyoutletonline.net/ age just http://www.airjordans.us/ enthusiastic, http://www.nike-free-run.de/ you http://www.thomas-sabo.com.de/ can http://www.salomon-schuhe.com.de/ play http://www.nikestore.us/ the http://www.burberry2016.co.uk/ most http://airmax.shoesoutlet.us.com/ beautiful music http://www.adidasshoes.top/ in http://www.christian-louboutinshoes.com.co/ the http://www.burberry-outletstore.net/ soul. http://www.ralphlaurenepolo.com/ Born http://www.burberry-handbags2016.in.net/ This paper declared the era http://www.fashionclothes.us.com/ of http://www.airmax-2015.org/ thick http://www.converse-shoesoutlet.com/ ink, it http://www.jimmy-choos.com/ is http://www.prada-handbags.com.co/ really Qing http://www.toms-shoes.com.co/ Sansei http://www.toryburch-sandals.in.net/ lucky, I http://www.longchamp-handbagsoutlet.us.com/ have the http://www.coco-chanels.us.com/ edge. http://www.nikeair-max.es/ Not to think http://www.mcmbags.net/ about what http://www.oakleyoutlet-online.us.com/ flat http://www.zxcoachoutlet.com/ Zeze, do http://www.coco-chanel.com.de/ not http://coachoutlet.euro-us.net/ repair what http://www.nike-huarache.nl/ suppression http://www.designerhandbagsoutlet.net.co/ suppression http://www.pumaonline-shop.de/ triumphantly, http://www.ray-ban-outlet.us.com/ play http://www.toms-shoesoutlet.net/ with http://www.ralphlaurenoutletonline.us.org/ my http://www.truereligion-outlet.us.org/ ink, paper http://www.nikemercurial.in.net/ and http://www.uggs.co.nl/ ink, http://www.true-religion.com.co/ ink http://www.michaelkors-outlet-online.com.co/ into http://www.michaelkorsbags.us.org/ song. Wenbujiadian, http://www.hollisters-canada.ca/ Tengjiao http://www.oakley-sunglasses-canada.ca/ Qifeng. http://www.ralphlaurenoutlet-online.us.com/ Three http://www.airmax-90.org/ feet http://www.giuseppe-zanotti.net/ of http://www.christianlouboutin.org.uk/ white http://www.pandora-charms.org.uk/ Xuan, lead http://www.oakley--sunglasses.com.au/ me http://www.ralphlaurenoutlet.us.com/ thousand words mantra; five bucket http://www.nikefree-run.org.uk/ heavy http://www.gucci-outletstore.com/ color, any http://www.vans-shoes.co.uk/ time http://airmax.misblackfriday.com/ I wanton http://www.ferragamoshoes.in.net/ chapters. http://www.prada.com.de/ Wenqi thunder, put http://www.uhren-shop.com.de/ pen http://www.new-balance.ca/ to http://www.michael--kors.us.com/ paper http://www.ralphlaurenpolos.in.net/ to http://www.tomsoutlet-online.com/ stay http://www.burberryoutlet2016.us.com/ hui, http://www.nike-shoes-canada.ca/ was http://www.cheap-michaelkors.com/ widely http://www.pandora.com.de/ bully http://www.adidas-superstar.nl/ Zimei, http://www.vans-shoes.net/ bearing http://www.beatsbydrdre.co.com/ pressure http://www.rosherun.org.uk/ Bai.

http://www.nike-air-max.com.au/

Post comment

Nick *
Your name or nickname
E-mail
Your contact information (optional, will not be shown)
Text *
Content of your comment
Calculate *
(* - required field)
STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2016 Adam Sawicki
Copyright © 2004-2016 Adam Sawicki