Does anyone know how to resize a pic and Save it as JPG (if it was a BMP)
Many thanks
Printable View
Does anyone know how to resize a pic and Save it as JPG (if it was a BMP)
Many thanks
I can give code to resize the picture in a picturebox.
But to store it as a jpeg, you will have to search on the web. I know there are add-ons that will do the job.
To resize you just use StretchBlt.
I've posted numerous examples on the forums. Here's one too ;
VB Code:
' By Jamie Plenderleith ' [email][email protected][/email] ' ' This code will stretch the 'background' picture of a form to the size of the form ' Change the picDc = GeneradeDC(..) line to point to an appropriate graphic ' Option Explicit Private picDc As Long Private picWidth As Long Private picHeight As Long Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long Public Function GenerateDC(FileName As String) As Long Dim DC As Long, picTemp As IPictureDisp DC = CreateCompatibleDC(0) If DC < 1 Then Exit Function End If Set picTemp = LoadPicture(FileName) picWidth = ScaleX(picTemp.Width) picHeight = ScaleY(picTemp.Height) SelectObject DC, picTemp DeleteObject picTemp Set picTemp = Nothing GenerateDC = DC End Function Private Sub Form_Load() ScaleMode = 3 picDc = GenerateDC("c:\jamie\barrk.bmp") With Picture1 .AutoRedraw = True .Move 0, 0, ScaleWidth, ScaleHeight .ScaleMode = 3 End With doStretch End Sub Private Function doStretch() Debug.Print StretchBlt(Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, picDc, 0, 0, picWidth, picHeight, vbSrcCopy) Picture1.Refresh End Function Private Sub Form_Resize() Picture1.Move 0, 0, ScaleWidth, ScaleHeight doStretch End Sub
Jamie/Chris
Thanks. I want to point a control at a (JPG or BMP file) on disk and resize it so that it is lets say 400x400 instead of its original size, then save it as a JPG file. Ultimately changing a JPG or BMP file from 1024x768(for example) to 800x600 lets say. Not just resize for the screen but ACTUALLY resize the image.
have you got any code that code do any of this?
:)
Blobby
Search on www.planetsourcecode.com for code for working with JPEGs
plenderleith ?
:eek:
hahahah
also,
blobby
have a look at this thread ;)
http://www.vbforums.com/showthread.php?s=&postid=702615
da_Silvy.....What is your real name? I cant keep calling you that!
Thanks for the info. Im a bit puzzled though.
It doesnt really say what the outcome was as some people had problems doing it the way suggested. All I can find in search engines are ActiveX/DLL's that you have to pay 100 bucks for.
It must be possible in that case so how come there is no code around to do it available for cut and paste?
I still have little information on whether it is actually possible at all.
Blob
I'm also very interested in this topic!
I developed a thumbnail viewer.
There is also a possibility to save the thumbs.
This happens as BMP, which consumes a lot of disk space of course...
I really wonder...is it possible to save as a jpeg?
With an API or whatever?
Christophe
Chris,
Yes, I need to load a pic in either BMP or JPG format and RESIZE it to a specific size and then save it as JPG.
I have searched the web for weeks and found NOTHING but DLL's you have to pay between 30 and 100 bucks for.
There has to be someone who knows how to do this!
If I find anything Ill let you know!
I'll post the code in a few minutes.
Jamie if you can do this, you are a God!
Putting the code together as we speak
Bet you cant do it really! :P
Hm, I found this link:
http://www.planet-source-code.com/xq...s/ShowCode.htm
It is a dll but it's free! I 'll give it a try, it looks good...
Chris,
yes i saw it but it uses a DLL from Intel. I wanted to do it with no 3rd party controls :)
Here ya go.
Read instructions before use.
Cheers jamie, ill check it out!! Prepare for a big girly kiss! :)
Big girly kiss ?
'tis a brave man that gives me a kiss I can tell ya ... ;)
But this uses also a third party OCX or what?
Yes it does.
I never said it didnt :D
Written in Delphi 4 with a memory leak and possibly a virus...
If a believe all the user comments on the site... :(
But I'll give it a try !
Thanks for the help!
My virus scanner never said nothing about no virus.
Anyway most of the people who post on planetsourcecode are absolute morons. I tested the jpg jobby here myself and it worked a dream.
Jamie,
Yeah, Norton 2002 said nothing about a virus.
Im not sure about the memory leak tho!
The only place a decent sized memory leak could be is in the GenerateDC function, and even thats not substantial (well not on mu laptop anyway).
Some people were saying on psc that they'd use it to convert 500 images or so. So I wouldn't worry about it.
But it matters to me!!!
I have to convert up to 10.000 images :D :D :D
It'll be grand.
I can do a test if you'd like
That's very nice of you but I don't want to occupy your computer for a whole day :D
10.000 images...count on +700 MB!:eek:
hang on.
Bombed after about the 700th 33kb bitmap
Hm, Ok, so this is not a solution for me :(
Perhaps I'll try that other dll I found...
Thanks for testing! ;)
I dont think the other link allows yu to resize the image. Only convert to JPG
True, but resizing is not a problem for me, that was already solved in a previous thread :)
A thought.
If you use an Image control set to a small size and use the STRETCH property = True then load a large Image.........Does that shrinke the image internally too or just the display version of it. ie. Is the image still its original size if you resave it or is it only now as big as the shrunk imagebox image?
In other words (if thats confusing) If you had an image box 100x100 on the screen set to Stretch=true and loaded a 1000x1000 image into it.....does it still contain the full image (1000x1000) if you resave the image1.Picture or is it now the reduced 100x100 picture that gets saved?
that'll stretch the imagebox, not the picture
Thats what i thought
Ta.
I use this to resize a picture in a picturebox:
Assume you have loaded a picture in a picturebox called picTmp.
Be sure autoredraw is False!
Now, set the width and height of the picturebox as you like:
picTmp.height = ...
picTmp.Width = ...
Now use the following code:
picTmp.AutoRedraw = True
picTmp.PaintPicture picTmp, 0, 0, picTmp.Width, picTmp.Height
Set picTmp = picTmp.Image
picTmp.AutoRedraw = False
This should do the trick.
Jamie, Ive tried your code bit the quality is.......well......not too good. The quality of the smaller picture looks nothing like the quality of the original. i was expecting it to be like VB when you load a 1024x768 image into a 400x300 picture box. The code gives quality similar to changing the colour depth to 256 colours.
Is there any other way of resizing the image and keeping the quality ?
The only way to improve it is it use SetStretchBltMode() with HALFTONE
Blimey it gets complicated dont it?
not really. hang on
The idea is that you call it before you do any stretching. It does work, not its not all that great ...
VB Code:
' StretchBlt() Modes Const BLACKONWHITE = 1 Const WHITEONBLACK = 2 Const COLORONCOLOR = 3 Const HALFTONE = 4 Private Declare Function SetStretchBltMode Lib "gdi32" (ByVal hdc As Long, ByVal nStretchMode As Long) As Long Private Sub Form_Load() SetStretchBltMode Picture1.hdc, HALFTONE End Sub
Is there anything you dont know? :)
ah im sure there's a few bits and pieces :)
ha ha very funny!!
Ok, I am doing an important project.
I need to allow the user to click on an image JPG or BMP
whatever they choose i want to resize (make smaller mostly) to make sure it is 489x489 pixels and then resave it as BMP or JPG
I was wondering if it was possible to use the IMAGE control to STRETCH (property) the picture so its square (489x489) and then copy the 489x489 pixels into a picturebox so i can use the SAVEPICTURE method on the picturebox.
Does this make sense?
It save calling StretchBlt etc.....
Hello Mr Jamie person
Do you know how easy it is to do this?
Yes, quite.
Gimme a few mins.
VB Code:
Option Explicit Private picDc As Long Private picWidth As Long Private picHeight As Long Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long Private Declare Function SetStretchBltMode Lib "gdi32" (ByVal hdc As Long, ByVal nStretchMode As Long) As Long ' StretchBlt() Modes Private Const BLACKONWHITE = 1 Private Const WHITEONBLACK = 2 Private Const COLORONCOLOR = 3 Private Const HALFTONE = 4 Private Const MAXSTRETCHBLTMODE = 4 Public Function GenerateDC(FileName As String) As Long Dim DC As Long, picTemp As IPictureDisp DC = CreateCompatibleDC(0) If DC < 1 Then Exit Function End If Set picTemp = LoadPicture(FileName) picWidth = ScaleX(picTemp.Width) picHeight = ScaleY(picTemp.Height) SelectObject DC, picTemp DeleteObject picTemp Set picTemp = Nothing GenerateDC = DC End Function Private Sub cmdDoodle_Click() With CommonDialog1 .ShowOpen If Not .FileName = "" Then Dim tempDc As Long picDc = GenerateDC(.FileName) If (picWidth > 489) Or (picHeight > 489) Then SetStretchBltMode Picture1.hdc, WHITEONBLACK StretchBlt Picture1.hdc, 0, 0, 489, 489, picDc, 0, 0, picWidth, picHeight, vbSrcCopy .ShowSave If Not .FileName = "" Then SavePicture Picture1.Image, .FileName MsgBox "Image saved" End If End If End If End With End Sub Private Sub Form_Load() ScaleMode = 3 With Picture1 .ScaleMode = 3 .Width = 489 .Height = 489 .AutoRedraw = True End With End Sub
Jamie,
You are the dogs bo******!!! It works fantastic!!!!
Had to make a couple of changes, including using the HALFTONE option but its fantastic!! All i need to do now is at some point look into converting the image to JPEG but at least my app works fine and the images are only 600K anyway. I can live with that!
If you were over here in sunny England and Id buy you all the beers you could drink. Thanks a lot mate....
I dont care what anyone else is saying about you, i think you are alright! :))
Yes I am joking so dont get paranoid!
Jamie,
One quick question..................The code doesnt ALWAYS work but I know why. The GenerateDC function sometimes returns zero (20% of the time). When the return value is zero the picture doesnt get saved or displayed. When its a positive Long, it does.
Why should this happen intermittently?
Should I 'while' it in a loop until the Return value is greater than zero?
I found that using WHITEONBLACK seemed to give better results, contradictory to documentation...
Um anyway regarding the GenerateDC() function. Hmm I've seen a few cases where its not perfect. I wrote the function myself, so perhaps there are better versions out there ?
Alternatively you could just add another picturebox, and use .LoadPicture to get an image into it, then stretchblt into the second picturebox
my name is dave ;)Quote:
Originally posted by Blobby
da_Silvy.....What is your real name? I cant keep calling you that!
Thanks for the info. Im a bit puzzled though.
It doesnt really say what the outcome was as some people had problems doing it the way suggested. All I can find in search engines are ActiveX/DLL's that you have to pay 100 bucks for.
It must be possible in that case so how come there is no code around to do it available for cut and paste?
I still have little information on whether it is actually possible at all.
Blob
Thanks for the contribution there dave ;)
no probs
what language is ur location in?
Gaeilge (Irish)
gaelic?
is that scottish?
Um basically the same I suppose