• 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 rene.nyffenegger@adp-gmbh.ch
    */


    #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 rene.nyffenegger@adp-gmbh.ch
    */

    #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_;
    }

  • 相关阅读:
    Go从入门到精通——函数(function)——把函数作为值保存到变量中
    Go从入门到精通——列表(list)——可以快速增删的非连续空间的容器
    Go从入门到精通——数组(固定大小的连续空间)
    Go从入门到精通——切片(slice)(动态分配大小的连续空间)
    Go从入门到精通——映射(map)——建立事物关联的容器
    Go从入门到精通——类型别名(Type Alias)
    Go从入门到精通——匿名函数——没有函数名字的函数
    愿我如星,子如月,夜夜流光,相皎洁 Kai
    文明6萌新初次过神标~纪念 Kai
    我们中国最伟大,最普遍,而且最永久的艺术就是男人扮女人。——鲁迅 Kai
  • 原文地址:https://www.cnblogs.com/honeynm/p/4695862.html
Copyright © 2020-2023  润新知