Solved

WIN32  Copying a BITMAP to a new BITMAP

Posted on 1999-01-05
2
464 Views
Last Modified: 2012-08-14
In c/c++ .win32 without using mfc, how can you have a bitmap, and create a new bitmap which contains only a section of the original?

Example:
Original Bitmap 100 X 100 pixels.  I want to create a 20 x 20 bitmap that contains the picture whose top left corner starts at 80,80 on the original bitmap.
0
Comment
Question by:Cov
2 Comments
 
LVL 1

Author Comment

by:Cov
Comment Utility
Edited text of question
0
 
LVL 2

Accepted Solution

by:
ryanvs earned 20 total points
Comment Utility
You need to copy the bitmap using BitBlt or StretchBlt to a new bitmap
and memory DC that you create.  Assuming you have an existing Bitmap
handle and DC handle, you would do the following:

HDC hdcMem;           // Memory Device for new bitmap
HBITMAP hbmCopy;      // The new bitmap copy
int xCopyWidth = 20, yCopyHeight = 20;  // New Dimensions
int xWidth = 100, yHeight = 100;  // Original Dimensions
int xStart = 80, yStart = 80;    // Starting Position in Original

hdcMem = CreateCompatibleDC(hdcOrginal);
hbmCopy = CreateCompatibleBitmap(hdcMem, xCopyWidth, yCopyHeight);

SelectObject(hdcMem, hbmCopy);
SetMapMode(hdcMem, GetMapMode(hdcMem));

// Copy Original Bitmap to Bitmap Copy
BitBlt(
        hdcMem,    // Bitmap Copy Memory
        0,         // x Starting Point of Copy
        0,         // y Starting Point of Copy
        xCopyWidth,  // Width of Copy
        yCopyHeight, // Height of Copy
        hdcOriginal, // Device to be copied
        xStart,      // x Starting Point of Source
        yStart,      // y Starting Point of Source
        SRCCOPY);

<....program code....>
// When finished free bitmap and memory space
DeleteObject(hbmCopy);
DeleteDC(hdcMem);


I hope this helps...
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Keep your audience engaged and get the most out of your next presentation with these quick Prezi tips.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Learn how to create interesting presentations by including videos to keep your audience engaged using Prezi. Select "Insert" from the top menu in your Prezi editor: Select "YouTube Video": Paste the video URL into the prompt: "Select "Insert":…
Add visuals to your Prezi to keep your audience engaged. Learn how to embed images, edit them, and more in this video micro tutorial. Select "Insert" from the top menu in your Prezi editor: Select "Image": A toolbar will pop in from the right …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now