Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. gdi+ example ?

gdi+ example ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++winformsgraphicsdata-structuresperformance
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    s987690
    wrote on last edited by
    #1

    how does one do this in gdi+, mfc - load png image into memory - access array of values, eg as int or rgb bytes and apply any changes to image, if this step is needed - rotate image - crop image - save as bmp i currently used CxImage, but it seems buggy - totally weird things happen after i use ::Rotate :(( Thank You

    M H 3 Replies Last reply
    0
    • S s987690

      how does one do this in gdi+, mfc - load png image into memory - access array of values, eg as int or rgb bytes and apply any changes to image, if this step is needed - rotate image - crop image - save as bmp i currently used CxImage, but it seems buggy - totally weird things happen after i use ::Rotate :(( Thank You

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Bitmap class constructors will load a png image (as well as Bitmap::FromFile()) Image::RotateFlip() will rotate and/or flip To crop, extract the desired portion from the source image into a second image (see below). There's a Graphics::DrawImage() overload that will do just that. Bitmap::Save() will save as any of the supported types (see below). This sample code should be enough to get you started...

      // Resize png to 320x240 bmp
      // The GDI+ Bitmap class is derived from the GDI+ Image class
      // I've made no assumptions on the src image dimensions
      // You should use Image::GetHeight()/Image::GetWidth() in real life :)

      Gdiplus::Bitmap SrcBitmap(L"C:\\test.png", FALSE);

      Gdiplus::Bitmap DstBitmap(320, 240, SrcBitmap.GetPixelFormat());

      Graphics DstGraphics(&DstBitmap);
      DstGraphics.DrawImage(&SrcBitmap, 0, 0, 320, 240);

      CLSID bmpClsid;
      GetEncoderClsid(L"image/bmp", &bmpClsid);

      DstBitmap.Save(L"C:\\test.bmp", &bmpClsid, NULL);

      // This utility ripped right from the SDK

      int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
      {
      UINT num = 0; // number of image encoders
      UINT size = 0; // size of the image encoder array in bytes

      ImageCodecInfo* pImageCodecInfo = NULL;

      GetImageEncodersSize(&num, &size);
      if(size == 0)
      return -1; // Failure

      pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
      if(pImageCodecInfo == NULL)
      return -1; // Failure

      GetImageEncoders(num, size, pImageCodecInfo);

      for(UINT j = 0; j < num; ++j)
      {
      if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
      {
      *pClsid = pImageCodecInfo[j].Clsid;
      free(pImageCodecInfo);
      return j; // Success
      }
      }

      free(pImageCodecInfo);
      return -1; // Failure
      }

      -- modified at 14:14 Sunday 21st January, 2007 edited for splelling

      1 Reply Last reply
      0
      • S s987690

        how does one do this in gdi+, mfc - load png image into memory - access array of values, eg as int or rgb bytes and apply any changes to image, if this step is needed - rotate image - crop image - save as bmp i currently used CxImage, but it seems buggy - totally weird things happen after i use ::Rotate :(( Thank You

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        Forgot one... You can use Bitmap::LockBits()/Bitmap::UnlockBits() to access the image bits. Mark

        1 Reply Last reply
        0
        • S s987690

          how does one do this in gdi+, mfc - load png image into memory - access array of values, eg as int or rgb bytes and apply any changes to image, if this step is needed - rotate image - crop image - save as bmp i currently used CxImage, but it seems buggy - totally weird things happen after i use ::Rotate :(( Thank You

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          You can use of GDI+ about this subjects but if you want to load and change some things on the image and save it you can use also of CImage class but for rotate GDI+ is better however its possible with CImage but GDI+ is easy.


          WhiteSky


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups