Bitmap: a C++ class

Bitmap: a C++ class
        
The five steps involved to draw a bitmap:

Load bitmap using LoadBitmap or LoadImage
    Create a memory DC
    Select the bitmap into the memory DC.
    StretchBlt or BitBlt from the memory DC to screen DC.
    Cleanup.

This class is used in

Fractal Generator
    Avi Examples

The header file
Bitmap.h

/*
   Bitmap.h

Copyright (C) 2002-2005 René Nyffenegger

This source code is provided ‘as-is‘, without any express or implied
   warranty. In no event will the author be held liable for any damages
   arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
   including commercial applications, and to alter it and redistribute it
   freely, subject to the following restrictions:

1. The origin of this source code must not be misrepresented; you must not
      claim that you wrote the original source code. If you use this source code
      in a product, an acknowledgment in the product documentation would be
      appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be
      misrepresented as being the original source code.

3. This notice may not be removed or altered from any source distribution.

René Nyffenegger [email protected]
*/

#ifndef BITMAP_H_
#define BITMAP_H_

#include <string>
#include <sstream>
#include <windows.h>

class Bitmap {
  public:
    Bitmap();
    Bitmap(std::string const& file_name);

operator HBITMAP() const;

protected:
    friend class MemoryDC;
    Bitmap(HBITMAP);
    HBITMAP bitmap_;
};

#endif

The implementation file
Bitmap.cpp

/*
   Bitmap.h

Copyright (C) 2002-2005 René Nyffenegger

This source code is provided ‘as-is‘, without any express or implied
   warranty. In no event will the author be held liable for any damages
   arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
   including commercial applications, and to alter it and redistribute it
   freely, subject to the following restrictions:

1. The origin of this source code must not be misrepresented; you must not
      claim that you wrote the original source code. If you use this source code
      in a product, an acknowledgment in the product documentation would be
      appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be
      misrepresented as being the original source code.

3. This notice may not be removed or altered from any source distribution.

René Nyffenegger [email protected]
*/

#include "Bitmap.h"

/*! \page bmps_in_memory drawing on Bitmaps in Memory
 *
 *   If you want to use bitmaps in memory and draw upon them,
 *   you have to follow these steps:
 *
 *   \li 1 Allocate a MemoryDC (this is a memory device context used for the drawing operations
 *         which a device context provides)
 *   \li 2 Create a CompatibleBitmap
 *   \li 3 Select this compatible Bitmap into the memory device context (the bitmap now becomes
 *         the surface for the drawing operations made onto the device context. Keep the
 *         return value for unselecting it again
 *   \li 4 do your drawings
 *   \li 5 unselect the selected bitmap
 *
 *
 * See the following code for an example.
 *
 *   \code
 
  MemoryDC memDc;
  CompatibleBitmap compBmp(memDc, width, height);
  Bitmap oldBmp = memDc.Select(compBmp);
       
  // Your drawing operations go here
 
  memDc.Select(oldBmp);

*   \endcode
 *
 *
 *
 */

Bitmap::Bitmap() : bitmap_(0) {}

Bitmap::Bitmap(std::string const& file_name) {
  bitmap_ = static_cast<HBITMAP>(::LoadImage(0, file_name.c_str(), IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION));
}

Bitmap::Bitmap(HBITMAP bmp) : bitmap_(bmp) {

}

Bitmap::operator HBITMAP() const {
  return bitmap_;
}

时间: 2025-01-17 07:56:11

Bitmap: a C++ class的相关文章

WPF笔记整理 - Bitmap和BitmapImage

项目中有图片处理的逻辑,因此要用到Bitmap.而WPF加载的一般都是BitmapImage.这里就需要将BitmapImage转成Bitmap 1. 图片的路径要用这样的,假设图片在project下的Images目录,文件名XXImage.png. pack://application:,,,/xxx;component/Images/XXImage.png 2. 代码: Bitmap bmp = null; var image = new BitmapImage(new Uri(this.X

【Android自学日记】关于Bitmap的理解和使用-不完整版

最近的Android自学刚好学习到异步线程的使用,对于开启异步线程加载网络图片中用到的Bitmap有点小蒙逼,这到底是个啥???所以我就自信的打开了百度!!以下就是我学习到的知识! 百度定义: 位图文件(Bitmap),扩展名可以是.bmp或者.dib.位图是Windows标准格式图形文件,它将图像定义为由点(像素)组成,每个点可以由多种色彩表示,包括2.4.8.16.24和32位色彩.例如,一幅1024×768分辨率的32位真彩图片,其所占存储字节数为:1024×768×32/(8*1024)

bitset bitmap 海量数据处理

bitmap:是一个十分有用的结构.所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素.由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省.      适用范围:可进行数据的快速查找,判重,删除,一般来说数据范围是int的10倍以下 基本原理及要点:使用bit数组来表示某些元素是否存在,比如8位电话号码 扩展:bloom filter可以看做是对bit-map的扩展 问题实例: 1)已知某个文件内包含一些电话号码,每个号码为8位数字,统计不

Android异步加载全解析之Bitmap

Android异步加载全解析之Bitmap 在这篇文章中,我们分析了Android在对大图处理时的一些策略--Android异步加载全解析之大图处理  戳我戳我 那么在这篇中,我们来对图像--Bitmap进行一个更加细致的分析,掌握Bitmap的点点滴滴. 引入 Bitmap这玩意儿号称Android App头号杀手,特别是3.0之前的版本,简直就是皇帝般的存在,碰不得.摔不得.虽然后面的版本Android对Bitmap的管理也进行了一系列的优化,但是它依然是非常难处理的一个东西.在Androi

基于Redis bitmap实现开关配置功能

作者:zhanhailiang 日期:2014-12-21 bitmap api SETBIT key offset value 对key所储存的字符串值,设置或清除指定偏移量上的位(bit). 位的设置或清除取决于value参数,可以是0也可以是1. 当key不存在时,自动生成一个新的字符串值. 字符串会进行伸展(grown)以确保它可以将value保存在指定的偏移量上. 当字符串值进行伸展时,空白位置以0填充. offset参数必须大于或等于0,小于2^32(bit映射被限制在512MB之内

基于Redis bitmap实现签到功能

作者:zhanhailiang 日期:2014-12-21 需求场景 Bitmap 对于一些特定类型的计算非常有效. 假设现在我们希望记录自己网站上的用户的上线频率,比如说,计算用户A上线了多少天,用户B上 线了多少天,诸如此类,以此作为数据,从而决定让哪些用户参加beta测试等活动--这个模式可以使 用SETBIT和BITCOUNT来实现. 比如说,每当用户在某一天上线的时候,我们就使用SETBIT,以用户名作为key,将那天所代表的网站 的上线日作为offset 参数,并将这个offset

Android艺术——Bitmap高效加载和缓存(1)

通过Bitmap我们可以设计一个ImageLoader,实现应该具有的功能是: 图片的同步加载:图片的异步加载:图片的压缩:内存缓存:磁盘缓存:网络获取: 1.加载 首先提到加载:BitmapFactory类提供了四类方法:decodeFile.decodeResource.decodeStream和decideByteArray.分别是文件系统.资源.输入流和字节数加载Bitmap对象. 2.压缩 如何进行图片的压缩?首先我们为什么图片压缩呢?因为很多时候ImageView尺寸小于图片原始尺寸

C# Bitmap类型与Byte[]类型相互转化

Bitmap   =>   byte[] Bitmap b = new Bitmap( "test.bmp "); MemoryStream ms = new MemoryStream(); b.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp); byte[] bytes= ms.GetBuffer(); //byte[] bytes= ms.ToArray(); 这两句都可以,至于区别么,下面有解释 ms.Close(); byte

Bitmap 内存优化

Android在加载大背景图或者大量图片时,经常导致内存溢出(Out of Memory  Error),本文根据我处理这些问题的经历及其它开发者的经验,整理解决方案如下(部分代码及文字出处无法考证):  方案一.读取图片时注意方法的调用,适当压缩  尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完成decode后,最终都是通过java层的createBitmap来完成的,需要消耗

Android开发之Bitmap的高效加载

BitmapFactory类提供了四类方法:decodeFile, decodeResource, decodeStream和decodeByteArray 分别用于支持从文件系统,资源,输入流以及字节数组中加载出一个Bitmap对象,前两者又间接调用了decodeStream 为了避免OOM,可以通过采样率有效的加载图片 如果获取采样率? 1.将BitmapFactory.Options的inJustDecodeBounds的参数设置为true并加载图片 2.从BitmapFactory.Op