Error using imgwin.inc

Started by Nicola, March 06, 2024, 01:44:47 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Zlatko Vid

QuoteAs I said, open an image file (jpg, png, gif) see its size, reduce it if necessary and save while also changing the quality

OK Nicola
 ...but as you said you need GUI window to see image which you loaded
normal way is to use FileDialog using filters .jpg  .png .gif to open this types
of files ..images..

also usual way is to use child window inside main window to look into opened image
then you need some calculations to set raster images to size you want
i am not sure what they are or are even supported by GDIplus functions ..maybe yes
etc...etc...
I think that here on forum is one ImageViewer example by Jose Roca ..i am not
sure but i think that is here somewhere ..
  •  

Nicola

#16
I fixed it.
Now it's working fine. I'm ashamed to say, there was no GDI+ activation. In fact, I saw that hstatus gave me the value 18 which corresponds precisely to this eventuality.

Now the program allows me to open an image file and save it by lowering its quality.
I have to try to change the size as well.


#case capital
def NULL null

uses filedialogs
uses MinWin
uses ImgWin


#lookahead   'for procedures

'// var for fileopen
indexbase 1
string sep=chr(0)
string filter="jpg"+sep+"*.jpg"
sys tipogui = OFN_EXPLORER 'or OFN_ALLOWMULTISELECT


'//initiate the Gdip //
  sys hr,hdlg,token
  GdiplusStartupInput StartupInput

  StartupInput.GdiplusVersion = 1
  hr=GdiplusStartup token, StartupInput, byval 0
  '
  if hr
    print "Error initializing GDIplus: " hex hr
    terminate
  end if
'//


  typedef void *pvoid
  '
  sys     hstatus
  sys     pGraphics,pImage,pBitmap,pStream,pStreamo,pThumbnail
  wstring strFileName
  sys     nThumbnailWidth,nThumbnailHeight,nwidth,nheight
  '
  'hStatus = GdipCreateFromHDC hdc, pGraphics
  '
  ' // Create an image and a thumbnail of the image.
  '
'strFileName = "../images/Dawn1815.jpg"
strFileName=FileDialog("C:\temp\1",filter,"Seleziona Immagine","", 0,tipogui,0)
'print strfilename
if strFileName ="" then
  print "errore file"
  ExitProcess(0)
endif

hStatus = GdipLoadImageFromFile strFileName, pImage
if hStatus<>0 then print "GdipLoadImageFromFile " hStatus

hStatus = GdipGetImageThumbnail pImage, 40, 40, pThumbnail, null, null

         'GdipCreateBitmapFromFile(strFileName, LPVOID pBitMap)
hStatus = GdipCreateBitmapFromFile(strFileName, *pBitMap)
if hStatus<>0 then  print "GdipCreateBitmapFromFile " hStatus
  if pBitMap=0 then
    print "no pBitMap"
    'ExitProcess(0)
  endif

  '
  ' // Draw the original and the thumbnail images.
  '
  hStatus = GdipGetImageWidth pImage, nWidth
  hStatus = GdipGetImageHeight pImage, nHeight
  hStatus = GdipDrawImageRectI pGraphics, pImage, 10, 60, nWidth, nHeight
  hStatus = GdipGetImageWidth pThumbnail, nThumbnailWidth
  hStatus = GdipGetImageHeight pThumbnail, nThumbnailHeight
  hStatus = GdipDrawImageRect pGraphics, pThumbnail, 10, 10, nThumbnailWidth, nThumbnailHeight


'SaveImage(string fname,typimg, int quality, LPVOID pBitMap)
 SaveImage("c:\temp\1\o2.jpg","image/jpeg",1, pBitMap)

  '
  ' // Cleanup
  '
  if pThumbnail
    GdipDisposeImage pThumbnail
  endif
  if pImage
    GdipDisposeImage pImage
  endif
  if pGraphics
    GdipDeleteGraphics pGraphics
  endif
  if pBitMap
    GdipDisposeImage pBitMap
  endif

  GdiplusShutdown token

int ids=ShellExecute(0,"open", "c:\temp\1\o2.jpg", "", "",1)

print "end"

Charles Pegge

#17
Hi Nicola,  Well done!

This is what I have, with size setting. It may not be a limitation for you but the quality seems to be automatically restricted for thumbnails.

'SaveThumbnail.o2bas / demo

#case capital
uses MinWin
uses ImgWin

  '========================================
     function SaveThumbnail(
       sys pImage=0,
       string fname="thumbnail.jpg",
       typimg="image/jpeg",
       int quality=50,
       width=100, height=100     ) as int
  '========================================
  '
  'typimg: image/jpeg image/png image/gif image/tiff image/bmp
  'quality: 1..100
  '
  int hstatus
  sys pThumbnail 'pimage for thumbnail
  % PixelFormat24bppRGB  137224
  % PixelFormat32bppARGB 2498570
  'http://forums.purebasic.com/english/viewtopic.php?p=205981
  if pImage
    'hStatus = GdipGetImageThumbnail pImage, 40, 40, pThumbnail, NULL, NULL

    hStatus = GdipGetImageThumbnail pImage, width, height, pThumbnail, 0, 0
    SaveImage fname,typimg,quality,pThumbnail
    GdipDisposeImage pThumbnail
    function=1
  endif
  end function


#case capital
uses MinWin
uses ImgWin
GDIplus 1
sys pImage
wstring file="../images/Dawn1815.jpg"
GdipLoadImageFromFile(file,pimage)
SaveThumbnail(pimage,quality=50,width=100,height=100) 'defaults
GdipDisposeImage pImage
GDIplus 0

Nicola

#18
Hi Charles,
In fact, last night I started looking at the various functions available and I found the same way that you are telling me.
Through thumbnails you can obtain and reduce the image as you wish. In the example I post, I take the size of the initial image and in the thumbnail I divide it in half and save the thumbnail with the desired quality.
At the same time, I tried another system. If you see there's a do... Loop. Through this loop I control the reduced image file size and reduce its quality until the file size reaches the value I want.
The whole thing works very well. I had to insert two functions, FILEEXISTS and FILELN.

Also I tried save without using pBitMap, but using pImage, it works the same with the same result.

I am very satisfied with what we have been able to do.


'Open and Save
'usa GDI+
'Adattamento di Nicola Piano
'08/03/2024
'============================

#case capital
def NULL null

uses filedialogs
uses MinWin
uses ImgWin


#lookahead   'for procedures

function FileExists(char*fi) as int
===================================
WIN32_FIND_DATA fd
sys h
h=FindFirstFile fi,fd
if h>0 then
  FindClose h
  return 1
end if
end function

function Fileln(char*fi) as int
===============================
sys f
int le
if FileExists(fi)<>1 then return 0
f=fopen fi,"r"
if f
  int n=_fileno f
  le = _filelength n
  'print le
  fclose f
  return le 
end if
end function


'// var for fileopen
indexbase 1
string sep=chr(0)
string filter="jpg"+sep+"*.jpg"
sys tipogui = OFN_EXPLORER 'or OFN_ALLOWMULTISELECT


'//initiate the Gdip //
  sys hr,hdlg,token
  GdiplusStartupInput StartupInput

  StartupInput.GdiplusVersion = 1
  hr=GdiplusStartup token, StartupInput, byval 0
  '
  if hr
    print "Error initializing GDIplus: " hex hr
    terminate
  end if
'//


  typedef void *pvoid
  '
  sys     hstatus
  sys     pGraphics,pImage,pBitmap,pStream,pStreamo,pThumbnail
  wstring strFileName
  sys     nThumbnailWidth,nThumbnailHeight,nwidth,nheight
  '
  'hStatus = GdipCreateFromHDC hdc, pGraphics
  '
  ' // Create an image and a thumbnail of the image.
  '
'strFileName = "../images/Dawn1815.jpg"
strFileName=FileDialog("C:\temp\1",filter,"Seleziona Immagine","", 0,tipogui,0)
'print strfilename
if strFileName ="" then
  print "errore file"
  ExitProcess(0)
endif


hStatus = GdipLoadImageFromFile strFileName, pImage
if hStatus<>0 then print "GdipLoadImageFromFile " hStatus

  hStatus = GdipGetImageWidth pImage, nWidth
  hStatus = GdipGetImageHeight pImage, nHeight

'hStatus = GdipGetImageThumbnail pImage, 40, 40, pThumbnail, null, null
hStatus = GdipGetImageThumbnail pImage, nWidth/2, nHeight/2, pThumbnail, null, null

         'GdipCreateBitmapFromFile(strFileName, LPVOID pBitMap)
hStatus = GdipCreateBitmapFromFile(strFileName, *pBitMap)
if hStatus<>0 then  print "GdipCreateBitmapFromFile " hStatus
  if pBitMap=0 then
    print "no pBitMap"
    'ExitProcess(0)
  endif

  '
  ' // Draw the original and the thumbnail images.
  '

  hStatus = GdipDrawImageRectI pGraphics, pImage, 10, 60, nWidth, nHeight
  hStatus = GdipGetImageWidth pThumbnail, nThumbnailWidth
  hStatus = GdipGetImageHeight pThumbnail, nThumbnailHeight
  hStatus = GdipDrawImageRect pGraphics, pThumbnail, 10, 10, nThumbnailWidth, nThumbnailHeight

wstring outFname="c:\temp\1\o2.jpg"
int qualita=20, dimage=100000
'riduce la qualità fino a che la dimensione del file è quello desiderato.
do
  qualita-=1
  'SaveImage(string fname,typimg, int quality, LPVOID pBitMap)
  SaveImage(outFname,"image/jpeg",qualita, pBitMap)
  'SaveImage("c:\temp\1\o2_.jpg","image/jpeg",1, pImage)

loop until Fileln(outFname)<=dimage 

  SaveImage("c:\temp\1\o2_half.jpg","image/jpeg",20, pThumbnail)  'funziona
  '
  ' // Cleanup
  '
  if pThumbnail
    GdipDisposeImage pThumbnail
  endif
  if pImage
    GdipDisposeImage pImage
  endif
  if pGraphics
    GdipDeleteGraphics pGraphics
  endif
  if pBitMap
    GdipDisposeImage pBitMap
  endif

  GdiplusShutdown token

int ids=ShellExecute(0,"open", "c:\temp\1\o2.jpg", "", "",1)

print "end"