I have a commandbutton that reads a bitmap file and shows its picture enlarged 4 times in a Picturebox named Picture2. (There is no Picture1 yet. There is only a PictureBox named Picture2 for now)
The enlargement is done using GDIPlus techniques.
Here is a the code, and it works perfectly:
But then I copied the above code and pasted it under another commandbutton, and tried to change it.Code:Private Sub cmdShowPic3_Click() Dim DoScale As Boolean Dim Render As Boolean Dim hGraphics As Long Dim hMatrix As Long Dim tHandle As Long Dim destX As Long Dim destY As Long Dim destWidth As Long Dim destHeight As Long Dim srcX As Long Dim srcY As Long Dim srcWidth As Long Dim srcHeight As Long Dim srcRect As RECTF Dim GDIplusEffectsHandle As Long Dim file_name As String Dim scalerX As Single Dim scalerY As Single Dim ZoomFactor As Long Const UnitPixel As Long = 2& DoScale = True Picture2.AutoRedraw = True GDIplusEffectsHandle = 0& ZoomFactor = 4# file_name = "C:\MyFiles\MyPics\temp1\pic001.bmp" scalerX = ZoomFactor scalerY = ZoomFactor If Not DoScale Then ZoomFactor = 1 End If Picture2.Height = Picture2.Height * ZoomFactor Picture2.Width = Picture2.Width * ZoomFactor If m_Image Then GdipDisposeImage m_Image: m_Image = 0 If GdipLoadImageFromFile(StrPtr(file_name), m_Image) Then ' error; failed to load image Exit Sub End If If GdipCreateFromHDC(Picture2.hdc, hGraphics) Then ' must release DC with GdipDeleteGraphics ' error; should never get this if passing a valid DC Exit Sub End If Call GdipSetInterpolationMode(hGraphics, RenderInterpolation.lvicHighQualityBicubic) Call GdipSetPixelOffsetMode(hGraphics, SmoothingModeAntiAlias) destX = 0 destY = 0 destWidth = 150 ' * ZoomFactor destHeight = 150 ' * ZoomFactor srcX = 0 srcY = 0 srcWidth = 150 ' * ZoomFactor srcHeight = 150 ' * ZoomFactor srcRect.nTop = srcY ' * ZoomFactor srcRect.nLeft = srcX ' * ZoomFactor srcRect.nHeight = srcHeight ' * ZoomFactor srcRect.nWidth = srcWidth ' * ZoomFactor If DoScale Then Call GdipCreateMatrix2(scalerX, 0&, 0&, scalerY, destX, destY, hMatrix) Render = (GdipDrawImageFX(hGraphics, m_Image, srcRect, hMatrix, GDIplusEffectsHandle, 0, UnitPixel) = 0&) Else GdipDrawImageRectRectI hGraphics, m_Image, destX, destY, destWidth, destHeight, srcX, srcY, srcWidth, srcHeight, UnitPixel, 0, 0, 0 End If GdipDeleteGraphics hGraphics If hMatrix Then GdipDeleteMatrix hMatrix Picture2.Refresh End Sub
The change that I need is that instead of getting the picture from a file (as in the above code), I need to get the picture from another PictureBox (named Picture1) that has been already showing a picture on the screen.
In other words, a picture is already showing in a PictureBox named Picture1, and I need to show an enlarged copy of that (enlarged by a factor of 4) in another PictureBox named Picture2.
Here is the new code (under a separate commandbutton):
Code:Private Sub cmdShowPic5_Click() Dim DoScale As Boolean Dim Render As Boolean Dim hGraphics As Long Dim hMatrix As Long Dim tHandle As Long Dim destX As Long Dim destY As Long Dim destWidth As Long Dim destHeight As Long Dim srcX As Long Dim srcY As Long Dim srcWidth As Long Dim srcHeight As Long Dim srcRect As RECTF Dim GDIplusEffectsHandle As Long Dim file_name As String Dim scalerX As Single Dim scalerY As Single Dim ZoomFactor As Long Const UnitPixel As Long = 2& DoScale = True Picture2.AutoRedraw = True Picture2.Picture = Picture1.Picture Picture2.Height = Picture1.Height * ZoomFactor Picture2.Width = Picture1.Width * ZoomFactor GDIplusEffectsHandle = 0& ZoomFactor = 4# scalerX = ZoomFactor scalerY = ZoomFactor If Not DoScale Then ZoomFactor = 1 End If If m_Image Then GdipDisposeImage m_Image ' m_Image = Picture2.Image.hPal m_Image = Picture2.Image.Handle ' If GdipLoadImageFromFile(StrPtr(file_name), m_Image) Then ' ' error; failed to load image ' Exit Sub ' End If If GdipCreateFromHDC(Picture2.hdc, hGraphics) Then ' must release DC with GdipDeleteGraphics ' error; should never get this if passing a valid DC Exit Sub End If Call GdipSetInterpolationMode(hGraphics, RenderInterpolation.lvicHighQualityBicubic) Call GdipSetPixelOffsetMode(hGraphics, SmoothingModeAntiAlias) destX = 0 destY = 0 destWidth = 150 ' * ZoomFactor destHeight = 150 ' * ZoomFactor srcX = 0 srcY = 0 srcWidth = 150 ' * ZoomFactor srcHeight = 150 ' * ZoomFactor srcRect.nTop = srcY ' * ZoomFactor srcRect.nLeft = srcX ' * ZoomFactor srcRect.nHeight = srcHeight ' * ZoomFactor srcRect.nWidth = srcWidth ' * ZoomFactor If DoScale Then Call GdipCreateMatrix2(scalerX, 0&, 0&, scalerY, destX, destY, hMatrix) Render = (GdipDrawImageFX(hGraphics, m_Image, srcRect, hMatrix, GDIplusEffectsHandle, 0, UnitPixel) = 0&) Else GdipDrawImageRectRectI hGraphics, m_Image, destX, destY, destWidth, destHeight, srcX, srcY, srcWidth, srcHeight, UnitPixel, 0, 0, 0 End If GdipDeleteGraphics hGraphics If hMatrix Then GdipDeleteMatrix hMatrix Picture2.Refresh End Sub
But this does NOT work, and I don't understand why.
If you note, the main change between the above pieces of code is that in the first one there is a section like this:
And in the second piece of code, I have replaced it with this:Code:If m_Image Then GdipDisposeImage m_Image: m_Image = 0 If GdipLoadImageFromFile(StrPtr(file_name), m_Image) Then ' error; failed to load image Exit Sub End If
because the image handle cannot come from the file, as there is no file in here and the original picture is already showing in Picture1 and copied into Picture2 by this line of code:Code:If m_Image Then GdipDisposeImage m_Image ' m_Image = Picture2.Image.hPal m_Image = Picture2.Image.Handle ' If GdipLoadImageFromFile(StrPtr(file_name), m_Image) Then ' ' error; failed to load image ' Exit Sub ' End If
So, I assumed that I could easily get the image handle like this:Code:Picture2.Picture = Picture1.Picture
or like this:Code:m_Image = Picture2.Image.hPal
And nothing works.Code:m_Image = Picture2.Image.Handle
What is it that I am missing?
How can I fix this?
thanks,
Ilia


Reply With Quote

