[C#] UserControl not displayed in toolbox

[ UserControl not show in toolbox ]
When you create a UserControl, you'll usually see it right in the toolbox after Rebuild Project.
But sometimes you will not see your UserControl in the toolbox, to solve it you can do in the following way:

Open Microsoft Visual Studio 2008 and set this option:
Tools > Options > Windows Forms Designer > General : AutoToolboxPopulate

How to get size of CBitmap ?

After you Load an bitmap image into CBitmap, you can use this code to get its size.

      CBitmap bmp;
      bmp.LoadBitmap( /*Load bitmap*/ );
      BITMAP BmpInfo;
      bmp.GetBitmap(&BmpInfo);

      int iWidth = BmpInfo.bmWidth;
int iHeight = BmpInfo.bmHeight;

How to Load and Show an image on MFC ?

Load and Show an Image on MFC. In this way, you can load an *.bmp Image to show on a component on you MFC form

// Load an Bitmap Image


HBITMAP  ImageShower::LoadBitmapFromFile(CString path)
{
      HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,
                              path,
                              IMAGE_BITMAP,0,0,
                              LR_LOADFROMFILE|LR_CREATEDIBSECTION);
      return hBmp;
}
// Show an HBITMAP on an DC


void ImageShower::ShowBitmapOnDC(HBITMAP hBmp, CWnd* showPlace)
{
      CBitmap bmp;
      bmp.Attach(hBmp);

      BITMAP bmpInfo;
      bmp.GetBitmap(&bmpInfo);
     
      // Get rectangle of show place
      CRect rec;
      showPlace->GetWindowRect(&rec);
     
      // Initialize DC
      CClientDC dc(showPlace);
      CDC bmDC;
      bmDC.CreateCompatibleDC(&dc);
      CBitmap *pOldbmp = bmDC.SelectObject(&bmp);

      BITMAP  bi;
      bmp.GetBitmap(&bi);
      // Scale Image and show full on DC
      dc.StretchBlt(0,0,
            rec.Width(), rec.Height(),
            &bmDC,0,0,bmpInfo.bmWidth, bmpInfo.bmHeight,SRCCOPY);
bmDC.SelectObject(pOldbmp);
}

// How to use these functions ?
Example:

// Path of image
CString path("C:\\cLena.bmp");
// Load bitmap
HBITMAP hBmp = LoadBitmapFromFile(path);
// Get Handle of Item
CWnd* mP = GetDlgItem(IDC_STATIC);
// Show bitmap on Item
ShowBitmapOnDC(hBmp,mP);

How to Convert IplImage* to HBITMAP

Some time you want to show the picture and the result picture that process by OpenCV (IplImage*) on MFC form, so you must convert IplImage* to HBITMAP. I think have many way to do that, but this is a simplest way, you can try it:

HBITMAP ImageShower::ConvertIplImage2HBITMAP(IplImage* pImage)
{
      IplImage* image = (IplImage*)pImage;
      bool imgConverted = false;
      /*if(pImage->nChannels!=3)
      {
            IplImage* imageCh3 = cvCreateImage(cvGetSize(pImage),
                  8, 3);
            if(pImage->nChannels==1)
                  cvCvtColor(pImage, imageCh3, CV_GRAY2RGB);
            image = imageCh3;

            imgConverted = true;
      }*/

      int bpp = image->nChannels * 8;
      assert(image->width >= 0 && image->height >= 0 &&
            (bpp == 8 || bpp == 24 || bpp == 32));
      CvMat dst;
      void* dst_ptr = 0;
      HBITMAP hbmp = NULL;
      unsigned char buffer[sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD)];
      BITMAPINFO* bmi = (BITMAPINFO*)buffer;
      BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);

      ZeroMemory(bmih, sizeof(BITMAPINFOHEADER));
      bmih->biSize = sizeof(BITMAPINFOHEADER);
      bmih->biWidth = image->width;
      bmih->biHeight = image->origin ? abs(image->height) :
            -abs(image->height);
      bmih->biPlanes = 1;
      bmih->biBitCount = bpp;
      bmih->biCompression = BI_RGB;

      if (bpp == 8) {
            RGBQUAD* palette = bmi->bmiColors;
            int i;
            for (i = 0; i < 256; i++) {
                  palette[i].rgbRed = palette[i].rgbGreen = palette
                        [i].rgbBlue
                        = (BYTE)i;
                  palette[i].rgbReserved = 0;
            }
      }

      hbmp = CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, &dst_ptr, 0,
            0);
      cvInitMatHeader(&dst, image->height, image->width, CV_8UC3,
            dst_ptr, (image->width * image->nChannels + 3) & -4);
      cvConvertImage(image, &dst, image->origin ? CV_CVTIMG_FLIP : 0);

      if(imgConverted)
            cvReleaseImage(&image);

      return hbmp;
}

[C#] Write Text File

This function creates a text file when it runs. In the directory where you give in filePath and strContent you want to write. If you view the contents of this file, you'll see the following textual representation of the date and time when the program last ran.

void WriteTextFile(string filePath, string strContent)
{
    StreamWriter sW = new StreamWriter(filePath);
    sW.Write(strContent);           // Not line breaks
    //sW.WriteLine(strContent);     // Write and line breaks
    sW.Write(DateTime.Now);         // Write Date

    // Flush and Close
    sW.Close();
}

[C#] Read Text file Line by Line

Here is the code to read a text file from disk one line at a time into a string.  This code ensures the file exists and properly closes the file if an exception occurs.
Use these namespaces

using System;
using System.IO;

// Use this function

        private void ReadTextFile(string filePath)
        {
            // initialize line to fill content each line
            string line;
            if (File.Exists(filePath))      // Check existence
            {
                StreamReader file = null;   // initialize Reader
                try
                {
                    file = new StreamReader(filePath);
                    // Read line by line, stop if null.
                    while ((line = file.ReadLine()) != null)   
                    {
                        // === Use each line ===
                        Console.WriteLine(line);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                finally
                {
                    if (file != null)
                        file.Close();
                }
            }
        }

How to Convert CString to string ?

Convert CString to string ...
You can get CString from many function that used in C++ and MFC, (ex: GetDlgItemText()). In some case, another function requires string type. Use can use this code

LPCTSTR Convert_CString2String (CString strVar)
{
      // Convert CString to CW2A
      CT2CA pszConvertedAnsiString (strVar);
      // Use CW2A to string
            string strResult(pszConvertedAnsiString);
     
return strResult;
}