Re: Zoom in/ Zoom out images

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



See below...

On 4 Dec 2006 23:24:27 -0800, "Klic" <rohit.turambekar@xxxxxxxxx> wrote:

Hi Joe,

I have done changes as per ur reply. As i am refering to
the the Article

Displaying a JPG in your MFC Application
Paul DiLascia
article from MSDN Magezine

This example is in Doc/View Arcitecture and I am doing a
simple Dialog based application.
As this is Doc/View I cant traceout the actual flow of the programme
and Also cant able to implement it in my Dialog.
So can you plzzzzzz direct me a simple way to do it.
This is I have to do,
1..To show (.jpg,.Jpeg, .bmp) images in Tree Control.
****
You would have to use a custom-draw tree control. I'm sure there must be codeproject
articles on this. I've not done it. Also, you probably want to display thumbnails, and I
know there are articles on the creation of thumbnails in codeproject
****
2..When user cliks on any file that image shoould be shown in
Picture control in the same dialog.
****
This is trivial. When you get a selection, you identify the image, and then set that
image into the picture control of your choice. All you need is a jpeg-to-bitmap conversion
routine, which I think is wht that article was discussing.
****
3..Dialog has two buttons ZOOM IN and ZOOM OUT . When user klic on
the button the image from the picture contrl also should be change as
per button.
****
I would be inclined to use mapping modes instead of StretchBlt. You can download my
Viewport Explorer to see how mapping modes work, and even get some ideas about how to drag
the image around.
****

So plz tell me What changes I should do in my apllication.
I am thinking that i will not refer to above mentioned article as it
have complex flow.
So Plz help me.

Klic


================================================================
Joseph M. Newcomer wrote:
Diito. I forgot the * in my correction to your code.
joe

On 4 Dec 2006 07:02:21 -0800, "Klic" <rohit.turambekar@xxxxxxxxx> wrote:

sorry typing mistake................
m_wndPict.LoadImage( *szFilename);

Klic
==========================================================
Klic wrote:
Hello Joe,
Thank you very much for such elaborative explaination.
But some little complications are there.
Actually, I want to display images of type .bmp,.jpg,.jpeg and for that
i m using IPicture and OleLoadPicure.
As per your reply i have changed my code.
i hv made following changes....
1. I added class in project
class CPictureCtrl : public CStatic
{

2. I added class in project
class CPicture
{

3 In CPictureCtrl class by using the picture class object i am calling
the Render() method to draw a picture. But It doesnt draw an picture
on Dialog.

This is my modified ShowImages funcion

void CImageViewerDlg::ShowImages1(WPARAM wParam, LPARAM lParam)
{
CString *szFilename = (CString *)wParam;

/////////////////CPictureCtrl m_wndPict;

if(!m_wndPict.m_hWnd)
{
if(m_wndPict.SubclassDlgItem(IDC_SIMAGE,this))
{

m_wndPict.LoadImage(LPCSTR("frnd.bmp"));

}
}
}

Actually i am referring to
Displaying a JPG in your MFC Application
Paul DiLascia
article from MSDN .

Klic
============================================================
Joseph M. Newcomer wrote:
See below...
On 2 Dec 2006 05:43:23 -0800, "Klic" <rohit.turambekar@xxxxxxxxx> wrote:


Ajay Kalra wrote:
I am getting a null handle for

HBITMAP hBmp = m_ctlImage.GetBitmap();
where m_ctlImage is my static picture control style - frame



Is m_cltImage subclassed correctly. At what point/where are you calling
this. Use ASSERT_VALID on it to see if its valid object.

---
Ajay
=================================================



Thank you for Reply,
But
This is my code to load image-------------------------------------
ShowImages(Cstring *strPath)
****
And exactly what class does this method belong to?

Why are you passing a CString *? Why not a CString? Or for best performance, a const
CString &? Stop thinking in terms of C on the PDP-11. The only reason to pass a CString
* would be if the pointer could be NULL, and since you don't test for that condition, it
obviously cannot arise.
****
{

CString *szFilename =strPath;
****
Why do you make a copy of the parameter? This seems rather pointless
****

hBmp = (HBITMAP)::LoadImage(NULL,*szFilename,
IMAGE_BITMAP,0,0,
LR_LOADFROMFILE);

CBitmap bmp;
****
THis is a local variable
****

if(hBmp)
{
//Delete the current bitmap
if(bmp.DeleteObject())
****
Why? It's a LOCAL VARIABLE! It has nothing attached to it!
****
{ //Detach previous bitmap
bmp.Detach();
}
//Attach new loaded
bmp.Attach(hBmp);
****
As soon as you leave this function the destructor is called and the bitmap object is
deleted. Your CBitmap object must live as long as the image needs to live. Recode.
****
}
else
{
MessageBox("This file is Invalid.");
return;
}

***
Why are ;you doing this here? The drawing must be done in the OnPaint handler. If it
isn't, as soon as anything covers your window, the image is gone forever. You should
simply call Invalidate() and return.
****
CClientDC dc(this);
****
Since you will be moving this to the OnPaint handler, you will use the CPaintDC that is
declared there, and the above line can disappear.

bmDC.CreateCompatibleDC(&dc);

CBitmap * pOldbmp = bmDC.SelectObject(&bmp);

BITMAP bi;
bmp.GetBitmap(&bi);


CRect RectPicture;
m_ctlImage.GetWindowRect(&RectPicture);
ScreenToClient(&RectPicture);

// int x1,y1,x2,y2; class members
***
Why? They are only used here, and noplace else that you show. Also, how do you pan the
image? I would expect to see a SetWindowOrg or SetViewportOrg to handle the offset as you
pan the image when it is magnified beyond the size of the window. That pan value would be
kept as a class member, but values like this that you use once and never again don't need
to be class members.
***
x1 = RectPicture.TopLeft().x;
y1 = RectPicture.TopLeft().y;
x2 = bi.bmWidth;
y2 = bi.bmHeight;
iWidth = RectPicture.Width();
iHeight = RectPicture.Height();


dc.StretchBlt(x1,y1,iWidth,iHeight,&bmDC,0,0,x2,y2,SRCCOPY);;
bmDC.SelectObject(pOldbmp);
bmDC.Detach();
****
You are leaking DCs like crazy here. Why the Detach? The DC is now lost forever and can
never be reclaimed. I notice you don't get rid of the DC, which can never be used again
for any purpose, but you do get rid of the bitmap, which would be needed in the OnPaint
handler.
*****
}
===============================================
This is code for zoom in function

void CImageViewerDlg::OnBZoomIn()
{

CBitmap bmp;
// HBITMAP hBmp = m_ctlImage.GetBitmap();
****
Well, given the bitmap was destroyed, it is not surprising that it would be NULL
****

if(hBmp == NULL)
{
MessageBox("Please, select the Image.");
return;
}

bmp.Attach(&hBmp);

****
You are again confused. You must NOT do this drawing here! Do the drawing ONLY in the
OnPaint handler. Alll this routine would do would be to set the zoom factor and call
Invalidate. Rewrite it.
****
CClientDC dc(this);
CDC bmDC;
bmDC.CreateCompatibleDC(&dc);

CBitmap *pOldbmp = bmDC.SelectObject(&bmp);

BITMAP bi;
bmp.GetBitmap(&bi);

CRect RectPicture;
m_ctlImage.GetWindowRect(&RectPicture);
ScreenToClient(&RectPicture);



// static int iZoom;
iZoom += 50;

dc.StretchBlt(x1,y1,iWidth+iZoom,iHeight+iZoom,&bmDC,10,10,x2+iZoom,y2+iZoom,SRCCOPY);
bmDC.SelectObject(pOldbmp);
bmDC.Detach();
***
Same problem. You lose the bitmap, and leak the DC. Get rid of ALL of this code, move
all the logic to the OnPaint handler!
****

}

Now plz tell me where should i check ASSERT_VALID
Actually I am fresher and dont know much about GDI.
****
You have made several fundamental errors here: you have done drawing outside the OnPaint
handler. Remove ALL the drawing code shown here and move it to OnPaint. You put the
bitmap in a local variable, guaranteeing its destruction, and you leak DCs each time you
draw. Fix all of these
****

Plz tell me if u have better approach to to this.......i will be very
thanful to you.



Klic
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.


Quantcast