Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

copying part of a bitmap

[b][red]This message was edited by mad_hatter at 2002-9-30 15:48:22[/red][/b][hr]
hi, I've been trying to figure this out for 2 days now. What I want to do is open a .bmp file, and cut a piece of it out to paste into a different .bmp. Not sure how clear that is,

let's say this is the original bmp
**********
**********
**********
**********
**********

I want to cut the '8' section out
**********
**********
**********
******8888
******8888

and paste to a new bmp so it's smaller and only contains the graphics I need. like this
8888
8888

Hope someone has info on doing this. And thanks in advance for any help.


Comments

  • Josh CodeJosh Code Member Posts: 675
    : [b][red]This message was edited by mad_hatter at 2002-9-30 15:48:22[/red][/b][hr]
    : hi, I've been trying to figure this out for 2 days now. What I want to do is open a .bmp file, and cut a piece of it out to paste into a different .bmp. Not sure how clear that is,
    :
    : let's say this is the original bmp
    : **********
    : **********
    : **********
    : **********
    : **********
    :
    : I want to cut the '8' section out
    : **********
    : **********
    : **********
    : ******8888
    : ******8888
    :
    : and paste to a new bmp so it's smaller and only contains the graphics I need. like this
    : 8888
    : 8888
    :
    : Hope someone has info on doing this. And thanks in advance for any help.
    :


    1. Load the bitmap from the file into a bitmap variable, make a second bitmap with the dimensions of the section you want to get.

    2. Draw the larger bitmap onto the smaller one. In the example you gave, the x, y coordinates of the drawing would be negative.


    3. Now, you can do what you want with that section of the bitmap.
  • mad_hattermad_hatter Member Posts: 25
    : 1. Load the bitmap from the file into a bitmap variable, make a second bitmap with the dimensions of the section you want to get.
    :
    : 2. Draw the larger bitmap onto the smaller one. In the example you gave, the x, y coordinates of the drawing would be negative.
    :
    :
    : 3. Now, you can do what you want with that section of the bitmap.


    That's my problem, I've never worked with bitmaps. I've tried CBitmap, but I have no idea what to do with it. What I'm trying now is using a CFile, and copying only certain bits. haha this may be way out to left field, but so am I! I'm lost. Anyway, here's the code I have.

    [code]
    void CBitmapperDlg::OnOK()
    {

    CFile file;
    int size = 0;
    char *test = NULL;
    char *temp = NULL;
    LPCTSTR name = "foo.bmp";
    if(file.Open(name, CFile::modeRead | CFile::typeBinary) == TRUE)
    {
    size = file.GetLength();
    test= new char [size];
    temp = new char [size/2];
    int tempSize = size/2;
    memset(temp, 0, tempSize);
    memset(test, 0, size);
    // temp[0] = 'B';
    // temp[1] = 'M';
    // for(int i = 0;i < size;++i)
    {
    file.Read(test, size);
    }
    CFile nFile;
    LPCTSTR second = "test.bmp";

    if(!nFile.Open(second, CFile::modeWrite | CFile::modeCreate | CFile::typeBinary))
    AfxMessageBox("Couldn't open write file");

    for(int m = 0; m < 2800;m++)
    {
    temp[m] = test[m];
    }
    for(int k = tempSize+9000;k < size;k++)
    {
    //if(test[k] != -1)
    {
    int x = 2800;
    temp[x] = test[k];
    x++;
    }
    }
    nFile.Write(temp, tempSize - 2800);

    file.Close();
    nFile.Close();
    }
    else
    AfxMessageBox("didn't work");

    for(int j = 0;j<size;j++)
    {

    if(test[j] != -1)
    TRACE("test %d
    ",test[j], "
    j");


    }
    if(test != NULL)
    {
    delete [] test;
    test = NULL;
    }
    if(temp != NULL)
    {
    delete [] temp;
    temp = NULL;
    }
    CDialog::OnOK();

    [/code]

  • Josh CodeJosh Code Member Posts: 675
    : : 1. Load the bitmap from the file into a bitmap variable, make a second bitmap with the dimensions of the section you want to get.
    : :
    : : 2. Draw the larger bitmap onto the smaller one. In the example you gave, the x, y coordinates of the drawing would be negative.
    : :
    : :
    : : 3. Now, you can do what you want with that section of the bitmap.
    :
    :
    : That's my problem, I've never worked with bitmaps. I've tried CBitmap, but I have no idea what to do with it. What I'm trying now is using a CFile, and copying only certain bits. haha this may be way out to left field, but so am I! I'm lost. Anyway, here's the code I have.
    :
    : [code]
    : void CBitmapperDlg::OnOK()
    : {
    :
    : CFile file;
    : int size = 0;
    : char *test = NULL;
    : char *temp = NULL;
    : LPCTSTR name = "foo.bmp";
    : if(file.Open(name, CFile::modeRead | CFile::typeBinary) == TRUE)
    : {
    : size = file.GetLength();
    : test= new char [size];
    : temp = new char [size/2];
    : int tempSize = size/2;
    : memset(temp, 0, tempSize);
    : memset(test, 0, size);
    : // temp[0] = 'B';
    : // temp[1] = 'M';
    : // for(int i = 0;i < size;++i)
    : {
    : file.Read(test, size);
    : }
    : CFile nFile;
    : LPCTSTR second = "test.bmp";
    :
    : if(!nFile.Open(second, CFile::modeWrite | CFile::modeCreate | CFile::typeBinary))
    : AfxMessageBox("Couldn't open write file");
    :
    : for(int m = 0; m < 2800;m++)
    : {
    : temp[m] = test[m];
    : }
    : for(int k = tempSize+9000;k < size;k++)
    : {
    : //if(test[k] != -1)
    : {
    : int x = 2800;
    : temp[x] = test[k];
    : x++;
    : }
    : }
    : nFile.Write(temp, tempSize - 2800);
    :
    : file.Close();
    : nFile.Close();
    : }
    : else
    : AfxMessageBox("didn't work");
    :
    : for(int j = 0;j<size;j++)
    : {
    :
    : if(test[j] != -1)
    : TRACE("test %d
    ",test[j], "
    j");
    :
    :
    : }
    : if(test != NULL)
    : {
    : delete [] test;
    : test = NULL;
    : }
    : if(temp != NULL)
    : {
    : delete [] temp;
    : temp = NULL;
    : }
    : CDialog::OnOK();
    :
    : [/code]
    :

    JEEZ!

    Does that actually work? I don't know how to use bitmaps in c but there has to be an easier way to do it.

    There must be a procedure to load bitmap files into bitmap variables.







  • mad_hattermad_hatter Member Posts: 25
    Ok, I have the bitmap loaded. And I can get it to the clipboard using this code.

    [code]
    HDC hdc = NULL;
    hdc = CreateCompatibleDC(hdc);
    HBITMAP hbm;
    hbm = (HBITMAP)LoadImage(NULL, TEXT("foo.bmp"), IMAGE_BITMAP, 200, 200, LR_LOADFROMFILE);
    SelectObject( hdc, hbm);
    OpenClipboard();
    CBitmap bmp;
    SetClipboardData(CF_BITMAP, hbm);
    [/code]

    Which is what I want. Or well sort of, now I have to select only the bottom right corner of the bitmap load that into memory and copy it to a new bitmap. Any ideas?
  • Josh CodeJosh Code Member Posts: 675
    : Ok, I have the bitmap loaded. And I can get it to the clipboard using this code.
    :
    : [code]
    : HDC hdc = NULL;
    : hdc = CreateCompatibleDC(hdc);
    : HBITMAP hbm;
    : hbm = (HBITMAP)LoadImage(NULL, TEXT("foo.bmp"), IMAGE_BITMAP, 200, 200, LR_LOADFROMFILE);
    : SelectObject( hdc, hbm);
    : OpenClipboard();
    : CBitmap bmp;
    : SetClipboardData(CF_BITMAP, hbm);
    : [/code]
    :
    : Which is what I want. Or well sort of, now I have to select only the bottom right corner of the bitmap load that into memory and copy it to a new bitmap. Any ideas?
    :

    You can copy a section of the bitmap to another smaller bitmap using BitBlt or copyrect calls.
  • mad_hattermad_hatter Member Posts: 25
    ok I have what I want in a RECT. I know because it displayed on my dialog. My next problem is getting this RECT into a .bmp file. I'm still pretty lost with using DC's and DIB/DDB's. Here's the code I have for getting the graphix into my RECT. I tried putting it into the clipboard so I could paste it to a file but I get an error saying it couldn't get the data from the clipboard.

    Anyway, here's my code hope someone can help me how to get the RECT to a file on disk.

    [code]
    //set up variables
    CDC *dc = GetDC();;
    HDC hdc = dc->GetSafeHdc();
    HDC hdc2 = NULL;
    // RECT rcTemp; //temp rectangle
    static HDC hdcCompat; //DC for copying bmp
    static HDC hdcCompat2;
    static RECT rcBmp;
    static RECT rcTarget;
    static RECT rcClient;
    static HBITMAP hbmp, hbmp2;
    CBitmap bitmap, bitmap2;



    BOOL bVal = dc->Attach(hdc);//there's an assert error here and I dont' know why!!!
    if(!bVal)
    AfxMessageBox(_T("dc.Attach() failed"));

    //create my HDC
    hdc2 = CreateCompatibleDC(hdc);

    //load the .bmp into a HBITMAP
    // hbmp2 = (HBITMAP)LoadImage(NULL, TEXT("BITMAP1.bmp"), IMAGE_BITMAP, 300, 300, LR_CREATEDIBSECTION | LR_DEFAULTSIZE |

    // LR_LOADFROMFILE);

    hbmp2 = (HBITMAP)LoadImage(NULL, TEXT("foo.bmp"), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE |
    LR_LOADFROMFILE);

    //attach the handle to the CBitmap
    bitmap.Attach(hbmp2);
    SelectObject(hdc2, bitmap);

    //setup the RECT and fill it with the cropped bitmap coloring it with the bitmap
    BOOL ret = SetRect(&rcBmp, 690, 930, 1212, 1212);
    ret = FillRect(hdc, &rcBmp, CreatePatternBrush(hbmp2));

    /**************************************************************
    Up to here works, just not sure what to do with the rect once I get the info I want
    **************************************************************/

    //try setting to the clipboard, doesn't work.
    SelectObject(hdcCompat, &rcBmp);
    bitmap2.Attach(&rcBmp);
    CPalette pal;
    DDBToDIB(bitmap2, BI_ , pal);

    StretchBlt(hdcCompat2, 0, 0, 200, 200, hdcCompat, 0, 0, 200, 200, SRCCOPY);
    //put the RECT into a handle I can use
    hbmp = (HBITMAP)&rcBmp;
    //CreateDIBSection(hdcCompat2, NULL,
    //assign the handle to the hdc
    // hdcCompat = (HDC)hr;//(HDC)&rcBmp;

    //copy the filled hdc into the clipboard
    //hdc should contain the cropped section of the original bitmap. emphasis on "should"
    //hbmp = CreateCompatibleBitmap(hdcCompat, 150, 50);

    int err = -1;
    err = GetLastError();
    //open the clipboard and copy the
    ret = OpenClipboard();
    SetClipboardData(CF_BITMAP, hdcCompat2);//hbmp);

    err = GetLastError();
    //close clipboard and empty
    CloseClipboard();
    EmptyClipboard();

    [/code]
  • pvispvis Member Posts: 1
    : ok I have what I want in a RECT. I know because it displayed on my dialog. My next problem is getting this RECT into a .bmp file. I'm still pretty lost with using DC's and DIB/DDB's. Here's the code I have for getting the graphix into my RECT. I tried putting it into the clipboard so I could paste it to a file but I get an error saying it couldn't get the data from the clipboard.
    :
    : Anyway, here's my code hope someone can help me how to get the RECT to a file on disk.
    :
    : [code]
    : //set up variables
    : CDC *dc = GetDC();;
    : HDC hdc = dc->GetSafeHdc();
    : HDC hdc2 = NULL;
    : // RECT rcTemp; //temp rectangle
    : static HDC hdcCompat; //DC for copying bmp
    : static HDC hdcCompat2;
    : static RECT rcBmp;
    : static RECT rcTarget;
    : static RECT rcClient;
    : static HBITMAP hbmp, hbmp2;
    : CBitmap bitmap, bitmap2;
    :
    :
    :
    : BOOL bVal = dc->Attach(hdc);//there's an assert error here and I dont' know why!!!
    : if(!bVal)
    : AfxMessageBox(_T("dc.Attach() failed"));
    :
    : //create my HDC
    : hdc2 = CreateCompatibleDC(hdc);
    :
    : //load the .bmp into a HBITMAP
    : // hbmp2 = (HBITMAP)LoadImage(NULL, TEXT("BITMAP1.bmp"), IMAGE_BITMAP, 300, 300, LR_CREATEDIBSECTION | LR_DEFAULTSIZE |
    :
    : // LR_LOADFROMFILE);
    :
    : hbmp2 = (HBITMAP)LoadImage(NULL, TEXT("foo.bmp"), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE |
    : LR_LOADFROMFILE);
    :
    : //attach the handle to the CBitmap
    : bitmap.Attach(hbmp2);
    : SelectObject(hdc2, bitmap);
    :
    : //setup the RECT and fill it with the cropped bitmap coloring it with the bitmap
    : BOOL ret = SetRect(&rcBmp, 690, 930, 1212, 1212);
    : ret = FillRect(hdc, &rcBmp, CreatePatternBrush(hbmp2));
    :
    : /**************************************************************
    : Up to here works, just not sure what to do with the rect once I get the info I want
    : **************************************************************/
    :
    : //try setting to the clipboard, doesn't work.
    : SelectObject(hdcCompat, &rcBmp);
    : bitmap2.Attach(&rcBmp);
    : CPalette pal;
    : DDBToDIB(bitmap2, BI_ , pal);
    :
    : StretchBlt(hdcCompat2, 0, 0, 200, 200, hdcCompat, 0, 0, 200, 200, SRCCOPY);
    : //put the RECT into a handle I can use
    : hbmp = (HBITMAP)&rcBmp;
    : //CreateDIBSection(hdcCompat2, NULL,
    : //assign the handle to the hdc
    : // hdcCompat = (HDC)hr;//(HDC)&rcBmp;
    :
    : //copy the filled hdc into the clipboard
    : //hdc should contain the cropped section of the original bitmap. emphasis on "should"
    : //hbmp = CreateCompatibleBitmap(hdcCompat, 150, 50);
    :
    : int err = -1;
    : err = GetLastError();
    : //open the clipboard and copy the
    : ret = OpenClipboard();
    : SetClipboardData(CF_BITMAP, hdcCompat2);//hbmp);
    :
    : err = GetLastError();
    : //close clipboard and empty
    : CloseClipboard();
    : EmptyClipboard();
    :
    : [/code]
    :
    I want to know how can be made the same thing with a region of irregular form


  • Shawn CarterShawn Carter Member Posts: 0

    __ \ http://forcoder.org \ free video tutorials and ebooks about | C++ Scratch Visual Basic .NET Objective-C R C# C Delphi Assembly Java Ruby Swift Go Python PL/SQL MATLAB JavaScript Visual Basic Perl PHP Awk ABAP Kotlin Scala Bash Transact-SQL D VBScript SAS Rust Ada Julia F# Logo Fortran Prolog Hack Erlang LabVIEW Dart COBOL ML Scheme Clojure Crystal FoxPro Lisp Lua Apex Alice | __________

Sign In or Register to comment.