Prev: CRC32
Next: CAVORT20.DLL and 2.7
From: Saulo Rodrigues on 22 Nov 2006 05:38 Hi Dirk Thanks, I will give a try... btw, are you using something like that? TIA, Saulo Dirk (Belgium) escreveu: > Hi, > > If you have the jpeg in memory, you can do the following. > > Create a device = hDC > Paint the jpeg to this hDC > Read the hDC as a bitmap (see VO-SDk window{}:Print()-method) > Save the bitmap (see Microsoft SDK) > > Dirk > > On 20 Nov 2006 03:46:57 -0800, "Saulo Rodrigues" <rj.saulo(a)gmail.com> > wrote: > > >Hi everybody, > > > >With FabPaint I can create a jpeg file through a resource bitmap, > >however I'd like to make the inverse thing. Any help is welcome. > > > >TIA, > >Saulo
From: Sherlock on 22 Nov 2006 09:12 Saulo snip[ One could convert the JPG into BMP at startup of your app and the use it as a resource ? ] At the end of the data a JPG is basically an efficient image format... and when working with images like TIFF or JPG etc they should be converted in memory to DIB [ Device Independant Bitmap] and then saved again in the iamge format you wish to store it externally. Phil McGuinness -----
From: robertino on 22 Nov 2006 09:35 Here's a class I use to display a JPEG from (any) .JPG file, which might be useful: CLASS DIBControlNew INHERIT CustomControl PROTECT __oOrigin AS POINT PROTECT __oSize AS DIMENSION PROTECT pDIB AS PTR // The DIB... PROTECT lDIBLoaded AS LOGIC PROTECT nBX AS INT // Total width of borders in X-direction... PROTECT nBY AS INT // Total width of borders in Y-direction... PROTECT nImgSizeKB AS DWORD PROTECT cFullImgName AS STRING PROTECT _nSrcW, _nSrcH AS DWORD PROTECT _hDIBInfo AS _WINBITMAPINFO //DECLARE ACCESS BitMap // Access - returns Ptr to pDIB //DECLARE ACCESS ImagePath // The image passed to this DIB control. DECLARE ACCESS Height // Height of image DECLARE ACCESS Width // Width of image DECLARE METHOD LoadImage // METHOD Destroy() CLASS DIBControlNew DIBDelete(SELF:pDIB) SELF:pDIB := NULL_PTR SUPER:Destroy() RETURN NIL METHOD Expose(oExposeEvent) CLASS DIBControlNew LOCAL hDIBInfo AS _WINBITMAPINFO LOCAL oSize AS DIMENSION LOCAL nDestX, nDestY, nDestH, nDestW, nScanLines, nOldStretchMode AS INT LOCAL nScaleFactor AS FLOAT LOCAL hDC AS PTR LOCAL struPS IS _WinPaintStruct IF (oExposeEvent:Message == WM_PAINT) .AND. (oExposeEvent:Window == SELF) IF SELF:lDIBLoaded oSize := SELF:Size IF (_nSrcW >= _nSrcH) // Landscape format... nScaleFactor := (oSize:Width - SELF:nBX) / _nSrcW nDestH := INT(Integer(USUAL(_nSrcH * nScaleFactor))) nDestW := INT(Integer(USUAL(_nSrcW * nScaleFactor))) nDestX := 0 nDestY := ((oSize:Height - SELF:nBY) - nDestH) / 2 ELSE // Portrait format... nScaleFactor := (oSize:Height - SELF:nBY) / _nSrcH nDestH := INT(Integer(USUAL(_nSrcH * nScaleFactor))) nDestW := INT(Integer(USUAL(_nSrcW * nScaleFactor))) nDestX := ((oSize:Width - SELF:nBX) - nDestW) / 2 nDestY := 0 ENDIF BeginPaint(SELF:handle(), @struPS) hDC := GetDC(SELF:Handle()) nOldStretchMode := SetStretchBltMode(hDC, HALFTONE) nScanLines := StretchDIBits( hDC, ; nDestX, nDestY, nDestW, nDestH, ; 0, 0, _nSrcW, _nSrcH, ; @_hDIBInfo.bmiColors, ; _hDIBInfo, ; DIB_RGB_COLORS, ; SRCCOPY ) IF (nScanLines == GDI_ERROR) ErrorBox{, "StretchDIBits error"}:Show() ENDIF SetStretchBltMode(hDC, nOldStretchMode) ReleaseDC(SELF:Handle(), hDC) EndPaint(SELF:handle(), @struPS) ELSE InvalidateRect( SELF:Handle(), NULL_PTR, TRUE ) // This will clear the area in Image non-existent ENDIF // and removes image of previous item.. ENDIF SUPER:Expose(oExposeEvent) IF !InCollect() CollectForced() ENDIF RETURN NIL ACCESS Height AS DWORD PASCAL CLASS DIBControlNew RETURN _nSrcH METHOD Init(oOwner, xID, oPoint, oDimension, kStyle, lDataAware) CLASS DIBControlNew LOCAL cr IS _WINRECT LOCAL wr IS _WINRECT SUPER:Init(oOwner, xID, oPoint, oDimension, kStyle, lDataAware) SELF:__oOrigin := SELF:Origin SELF:__oSize := SELF:Size GetWindowRect(SELF:Handle(), @wr) GetClientRect(SELF:Handle(), @cr) SELF:nBX := wr.right - wr.left - cr.right SELF:nBY := wr.bottom - wr.top - cr.bottom SELF:lDIBLoaded := FALSE RETURN SELF METHOD LoadImage( cImgPath, cImgFile ) AS VOID PASCAL CLASS DIBControlNew LOCAL nDestX, nDestY, nDestH, nDestW AS INT LOCAL nFileSize AS INT LOCAL hDC AS PTR LOCAL oSize AS DIMENSION LOCAL nScaleFactor AS FLOAT LOCAL cErrMsg AS STRING SELF:lDIBLoaded := FALSE SELF:cFullImgName := cImgPath + "\" + cImgFile IF !Empty( cImgFile ) .AND. File( SELF:cFullImgName ) // Stop resource leakage - old image should be destroyed before new..... DIBDelete(SELF:pDIB) SELF:pDIB := NULL_PTR // Load from file..... SELF:pDIB := DIBCreateFromFile(String2Psz(SELF:cFullImgName)) IF (SELF:pDIB != NULL_PTR) _hDIBInfo := DIBGetInfo(SELF:pDIB) _nSrcH := _hDIBInfo.bmiHeader.biHeight _nSrcW := _hDIBInfo.bmiHeader.biWidth nFileSize := (FileSpec{SELF:cFullImgName}):Size SELF:nImgSizeKB := (nFileSize / 1000) SELF:lDIBLoaded := TRUE ELSE cErrMsg := "Error reading image: " + CRLF + GetLastSystemErrorMessage() MessageBox(0, String2Psz(cErrMsg), String2Psz("ERROR "),MB_ICONSTOP) ENDIF ELSE ENDIF InvalidateRect( SELF:Handle(), NULL_PTR, TRUE) RETURN ACCESS Width AS DWORD PASCAL CLASS DIBControlNew RETURN _nSrcW //ACCESS BitMap AS PTR PASCAL CLASS DibControlNew //RETURN SELF:pDIB CLASS MyDibNew INHERIT DIBControlNew METHOD MouseButtonDoubleClick(oMouseEvent) CLASS MyDibNew LOCAL nButtonID AS INT nButtonID := IIf(oMouseEvent == NULL_OBJECT, 0, oMouseEvent:ButtonID) SUPER:MouseButtonDoubleClick(oMouseEvent) //Put your changes here // For use when this control is used as a thumbnail viewer in the dw_MMDetails window..... IF IsMethod(SELF:owner, #ShowBigPicWin) SELF:owner:ShowBigPicWin() ENDIF RETURN NIL If you can extract something from that to open the file, you might then consider using something like CreateCompatibleDC and CreateCompatibleBitmap to convert to BMP - just an idea. HTH, Rob
From: Michael Rubinstein on 22 Nov 2006 09:47 Saulo, you are on the right track - you can store images in formats other then BMP in a resource. You don't really need FabPaint to create an image from a custom resource - CAPaint will do just fine. The code below is at least 6 years old: ----------- CLASS MyJPGBitmap INHERIT BITMAP METHOD Init(xSource,oOwner) CLASS MyJPGBitmap LOCAL hGlobal,pData,pResource,pBuffer AS PTR LOCAL dwImageSize AS DWORD LOCAL pBitmapInfo AS _winBitmapInfo LOCAL pBmiHeader AS _WINBITMAPINFOHEADER LOCAL pRGBQUAD AS _WINRGBQUAD LOCAL pDibObject AS PTR LOCAL dwImageSize AS DWORD pResource := FindResource(NULL,PTR(_CAST,xSource),PTR(_CAST,"MYJPG")) IF pResource != NULL_PTR pBuffer := LoadResource(NULL,pResource) IF pBuffer != NULL_PTR pDibObject := DIBCreateFromPTR(pBuffer,INT(SizeofResource(NULL,pResource))) dwImageSize := SizeofResource(NULL,pResource) //allocate memory on global heap hGlobal := GlobalAlloc(GMEM_MOVEABLE+GMEM_DISCARDABLE+GMEM_SHARE,dwImageSize) IF hGlobal != NULL //lock memory block and return pointer to the first byte pData := GlobalLock(hGlobal) //copy data MemCopy(pData,pBuffer,dwImageSize) GlobalUnlock(hGlobal) ENDIF ENDIF ENDIF IF pDibObject != NULL_PTR IF IsObject(oOwner) hOwner := oOwner:Handle() ELSE hOwner := NULL_PTR ENDIF hDC := GetDC(hOwner) pBitmapInfo := DIBGetInfo( pDibObject ) pBmiHeader := @pBitmapInfo.bmiHeader pRGBQUAD := @pBitmapInfo.bmiColors[1] IF SELF:hBitmap != NULL_PTR DeleteObject(SELF:hBitmap) ENDIF SELF:hBitmap := CreateDIBitmap(hDC,pBmiHeader,CBM_INIT,pRGBQUAD,pBitmapInfo,DIB_RGB_COLORS) ReleaseDC(hOwner,hDC) DIBDelete(pDibObject) ENDIF RETURN SELF ------------------------- I cut this code out of a library of mine that has more code. I might have cut too much. If you have problems I can assist you further. I use JPEG and GIF resources in most of my application. I don't use this particular code anymore, I use IPicture interface instead, but the technique of getting an image from a resource is the same. One thing to keep in mind is that Win32API functions that expect a BITMAP, ICON or CURSOR resource will reject a custom resource. Michael "Saulo Rodrigues" <rj.saulo(a)gmail.com> wrote in message news:1164132921.218938.311700(a)f16g2000cwb.googlegroups.com... > John, thanks for share your code, but that is not what I am looking > for. > Please let me try to explain it better: > > My real intention is do declare a resource entity: > For example: > RESOURCE MYJPG jpg c:\images\myjpg.jpg > > And so, like FabPaintLib:CreateFromResourceName(...) does: > something like: > CreateFromResourceName( _GetInst(), "MYJPG" ) > but this method only work for BMP's resources. > > Thanks again and sorry about your time > Saulo > > John Martens escreveu: > >> Saulo, >> >> Here's my function to save a picture file in another format. >> >> John >> >> >> FUNCTION PictureSaveAs(cFileNmOld AS STRING,cMode AS STRING) AS LOGIC >> * >> * function to save a file as DIB >> * base from the code of FabPaint >> * needs FabPaint Wrappers to use FabPaint.DLL instead of CaPaint.dll >> LOCAL lFuncResult := FALSE AS LOGIC >> LOCAL hf AS PTR >> LOCAL dwSize AS DWORD >> LOCAL pBmpBuffer AS PTR >> LOCAL pDib AS PTR >> LOCAL aKeuzeOpties := {} AS ARRAY >> LOCAL dwKeuze AS DWORD >> LOCAL cFileNmNew AS STRING >> * >> * see for the extension of the file >> DO CASE >> CASE ! File(cFileNmOld) >> * >> * file does not extist >> ToonMelding('W','Bestand '+cFileNmOld+' bestaat niet.<.><.>Bestand kan >> niet worden opgeslagen.',AlgShellWindow()) >> CASE Upper(LeesAlleenFileExt(cFileNmOld)) $ >> '|JPG|JPEG|TIF|TIFF|BMP|TGA|PNG|PCX|PCT|GIF|' >> * >> * get the pointer to the DIB >> hf := FOpen(cFileNmOld) >> IF hf = F_ERROR >> * >> * fileopen went wrong >> ToonMelding('SU','Bestand '+cFileNmOld+' is niet te openen door >> FOpen().<.><.>Bestand kan niet worden opgeslagen.',AlgShellWindow()) >> ELSE >> dwSize := FSeek(hf, 0, FS_END) >> FSeek(hf, 0, FS_SET) >> pBmpBuffer := MemAlloc(dwSize) >> DO CASE >> CASE pBmpBuffer = NULL_PTR >> * >> * ptr not right >> ToonMelding('SU','Bestand '+cFileNmOld+' geeft geen juiste >> pointer.<.><.>Bestand kan niet worden opgeslagen.',AlgShellWindow()) >> CASE ! FRead3(hf, pBmpBuffer, dwSize) == dwSize >> * >> * filesize not right >> ToonMelding('SU','Bestand '+cFileNmOld+' heeft niet de juiste >> afmeting.<.><.>Afmeting kan niet worden opgehaald.',AlgShellWindow()) >> OTHERWISE >> pDib := DIBCreateFromPTR(pBmpBuffer, dwSize) >> IF Empty(pDib) >> * >> * DIB creation was wrong >> ToonMelding('SU','Bestand '+cFileNmOld+' is niet te lezen door >> DIBCreateFromPTR().<.><.>Bestand kan niet worden >> opgeslagen.',AlgShellWindow()) >> ELSE >> * >> * kijken of soort op is gegeven >> IF 'Format(' $ cMode >> >> AAdd(aKeuzeOpties,{'',LeesLinksVan(LeesRechtsVan(cMode,'Format('),')')}) >> dwKeuze := 1 >> ELSE >> AAdd(aKeuzeOpties,{'JPEG formaat','JPG'}) >> AAdd(aKeuzeOpties,{'PNG formaat','PNG'}) >> AAdd(aKeuzeOpties,{'TIFF formaat','TIFF'}) >> AAdd(aKeuzeOpties,{'DIB formaat','DIB'}) >> dwKeuze := LeesKeuze('Kies formaat','Kies het formaat waarin >> '+cFileNmOld+' moet worden opgeslagen.',aKeuzeOpties,'',AlgShellWindow()) >> ENDIF >> * >> * nieuwe naam als basis opbouwen >> cFileNmNew := cFileNmOld >> DO WHILE '.' $ cFileNmNew .and. (! Right(cFileNmNew,1) == '.') >> cFileNmNew := LeesLinksVan(cFileNmNew,1) >> ENDDO >> * >> * save as >> DO CASE >> CASE 'JPG' == aKeuzeOpties[dwKeuze,2] >> cFileNmNew += 'JPG' >> FileDel(cFileNmNew) >> DIBSaveAsJPEG(pDib,String2Psz(cFileNmNew)) >> CASE 'PNG' == aKeuzeOpties[dwKeuze,2] >> cFileNmNew += 'PNG' >> FileDel(cFileNmNew) >> DIBSaveAsPNG(pDib,String2Psz(cFileNmNew)) >> CASE 'TIFF' == aKeuzeOpties[dwKeuze,2] >> cFileNmNew += 'TIFF' >> FileDel(cFileNmNew) >> DIBSaveAsTIFF(pDib,String2Psz(cFileNmNew)) >> OTHERWISE >> cFileNmNew += 'DIB' >> FileDel(cFileNmNew) >> DIBSaveAs(pDib,String2Psz(cFileNmNew)) >> ENDCASE >> IF File(cFileNmNew) >> lFuncResult := TRUE >> ELSE >> * >> * fileinfo was not right >> ToonMelding('W','Bestand '+cFileNmOld+' is niet op te slaan als >> '+cFileNmNew+'.<.><.>Bestand kan niet worden >> opgeslagen.',AlgShellWindow()) >> ENDIF >> *
From: Saulo Rodrigues on 22 Nov 2006 13:07
Thanks Michael, I just did some tests with your class, but I get a problem at the following line: SELF:hBitmap := CreateDIBitmap(hDC,pBmiHeader,CBM_INIT,pRGBQUAD,pBitmapInfo,DIB_RGB_COLORS) It returns a NULL_PTR value This is what I did: RESOURCE LOGOJPG MYJPG c:\images\image.jpg and: METHOD MyMethod CLASS MyDialogWindow LOCAL oJPG AS MyJPGBitMap oJPG := MyJPGBitmap{"LOGOJPG", SELF} Thanks in advance for your help. Saulo |