CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: printer not showing the image properly.

  1. #1
    Join Date
    Mar 2010
    Posts
    74

    printer not showing the image properly.

    Below is the code that prints image but image width and height is very less.
    Suppose if my image width = 900 and height=600 then it prints the image with width around 60 and height around 70 . What can be problem ?

    DWORD CDemo1Dlg:oPrintJob(LPTSTR szPrinterName)//szPrinterName is path of the printer in the network
    {
    DWORD dwError = 0;

    TCHAR szJobName[128] = {0};
    HDC hDC = 0;
    DOCINFO DocInfo;

    BITMAPINFO BmpInfo;
    int nRet = 0;

    HBITMAP hBmp24 = 0;
    BITMAP Bmp24 = {0};

    LOGFONT LogFont = {0};
    HFONT hFont = 0,
    hDefFont = 0;

    if ( !m_Bmp24Name.IsEmpty() )
    {
    hBmp24 = (HBITMAP) LoadImage(0, (LPCTSTR)m_Bmp24Name, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
    dwError = GetLastError();
    GetObject(hBmp24, sizeof(BITMAP), &Bmp24);
    }

    hDC = CreateDC(_T("WINSPOOL"), szPrinterName, NULL, 0);

    if ( dwError )
    return dwError;

    //4. StartDoc
    memset(&DocInfo, 0, sizeof(DOCINFO));
    DocInfo.cbSize = sizeof(DOCINFO);

    lstrcpy(szJobName, _T("Test"));
    DocInfo.lpszDocName = szJobName;

    nJobId = ::StartDoc(hDC, &DocInfo);

    //5. StartPage
    nRet = ::StartPage(hDC);

    //6.b Draw anything you want on page between ::StartPage and ::EndPage
    if ( Bmp24.bmBits )
    {
    memset(&BmpInfo, 0, sizeof(BITMAPINFO));
    BmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    BmpInfo.bmiHeader.biWidth = Bmp24.bmWidth;
    BmpInfo.bmiHeader.biHeight = Bmp24.bmHeight;
    BmpInfo.bmiHeader.biPlanes = Bmp24.bmPlanes;
    BmpInfo.bmiHeader.biBitCount = Bmp24.bmBitsPixel * Bmp24.bmPlanes;
    BmpInfo.bmiHeader.biCompression = BI_RGB;

    nRet = StretchDIBits(hDC, 0, 0, Bmp24.bmWidth, Bmp24.bmHeight, 0, 0, Bmp24.bmWidth, Bmp24.bmHeight, Bmp24.bmBits, &BmpInfo, DIB_RGB_COLORS, SRCCOPY);
    }

    //7. If page drawing is finished, set to end page
    nRet = ::EndPage(hDC);

    //8. If job done, set end of the job
    nRet = ::EndDoc(hDC);
    //release image object
    if ( hBmp24 )
    DeleteObject(hBmp24);

    :eleteDC(hDC);

    return 0;
    }

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,607

    Re: printer not showing the image properly.

    What can be problem ?
    The problem is in different device capabilities. Printer has physical resolution different from pixels, therefore you need to translate pixel sizes to device-specific sizes.
    Best regards,
    Igor

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: printer not showing the image properly.

    Quote Originally Posted by rohitnegi View Post
    Suppose if my image width = 900 and height=600 then it prints the image with width around 60 and height around 70
    900 of what? And 60 of what?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  4. #4
    Join Date
    Mar 2010
    Posts
    74

    Re: printer not showing the image properly.

    yes, after getting the device width and height using GetDeviceCaps(), Printer showing the image as per the printer page height and width. I am able to print the .bmp file but it does not print .jpg and .png images. how can i print .jpg and .png file format?.
    Last edited by rohitnegi; July 10th, 2012 at 08:28 AM.

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,607

    Re: printer not showing the image properly.

    Best regards,
    Igor

  6. #6
    Join Date
    Mar 2010
    Posts
    74

    Re: printer not showing the image properly.

    Using CImage i can print any kind of image. But when i try to print 4*6 image then images get distorted(stretch or shrink). What should i do?.
    Following is the code .

    void PrinterJob:rint(CString printerName,CString imageName,int width, int height)
    {
    TCHAR szJobName[128] = {0};
    HDC hDC = 0;
    DOCINFO DocInfo;
    int nJobId = 0;
    int nRet = 0;
    CImage image;
    //2. CreateDC
    hDC = CreateDC(_T("WINSPOOL"), printerName, NULL, 0);
    //4. StartDoc
    memset(&DocInfo, 0, sizeof(DOCINFO));
    DocInfo.cbSize = sizeof(DOCINFO);

    lstrcpy(szJobName, _T("Test"));
    DocInfo.lpszDocName = szJobName;

    nJobId = ::StartDoc(hDC, &DocInfo);

    nRet = ::StartPage(hDC);
    if(nRet)
    {
    image.Load(imageName);
    int xDpi =GetDeviceCaps(hDC, LOGPIXELSX);// to get the dpi
    int yDpi = GetDeviceCaps(hDC, LOGPIXELSY);
    printResult = image.StretchBlt(hDC,250,200,xDpi*width,yDpi*height, 0, 0, image.GetWidth(), image.GetHeight(), SRCCOPY);

    }
    //7. If page drawing is finished, set to end page
    nRet = ::EndPage(hDC);
    //8. If job done, set end of the job
    nRet = ::EndDoc(hDC);
    :eleteDC(hDC);

    }

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,607

    Re: printer not showing the image properly.

    images get distorted(stretch or shrink)
    Compared to image, Print's width and height should be corrected to have the same aspect ratio.
    Best regards,
    Igor

  8. #8
    Join Date
    Mar 2010
    Posts
    74

    Re: printer not showing the image properly.

    After using aspect ratio, printer showing images good.
    When i try to use this code to photo printer, it does not print the image. Do i have to modify the code to make it to work for photo printer?. I am using Hiti Photo Printer.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,607

    Re: printer not showing the image properly.

    It does not print without any error reported? Do you check error codes in your code?
    Best regards,
    Igor

  10. #10
    Join Date
    Mar 2010
    Posts
    74

    Re: printer not showing the image properly.

    My Code executes completely but HiTi photo printer gives warning "Ribon type mismatch" and error code 524800.

    that was problem from printer setting. It is working now after setting the page size same as ribbon size in the printer.
    Last edited by rohitnegi; August 8th, 2012 at 09:57 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)