Link to home
Start Free TrialLog in
Avatar of bemson
bemson

asked on

using disk instead of memory for image creation using intptr

I have the following code to re-create an image

the program fails due to lack of memory even though I have 2gb memory

How can I use disk space instead of memory which fails at this line:
Dim BitMapObject As IntPtr = TargetBitMap.GetHbitmap

is there another variable I can use instead of intptr?

thanks for any help

frmText.Text = "Creating New Image"

Dim FinalRectangle As New Rectangle(0, 0, S.Width, S.Height)

Dim TargetBitMap As New Bitmap(Original_Image, S.Width, S.Height)
TargetBitMap.SetResolution(ImageDPI, ImageDPI)

frmMain.Text = "Setting Image properties"
fails on next line "out of memory"
Dim BitMapObject As IntPtr = TargetBitMap.GetHbitmap


frmMain.Text = "Building New Image"

Dim NewGraphics As Graphics = Graphics.FromImage(TargetBitMap.FromHbitmap(BitMapObject))
NewGraphics.DrawImage(Original_Image, FinalRectangle)

frmText.Text = "Saving New Image"

TargetBitMap.Save(Image2Resize2, Imaging.ImageFormat.Jpeg)
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

What is the purpose of using "GetHbitmap" and "FromHbitmap"?...!

    Dim TargetBitMap As New Bitmap(Original_Image, S.Width, S.Height)
    Dim BitMapObject As IntPtr = TargetBitMap.GetHbitmap
    Dim NewGraphics As Graphics = Graphics.FromImage(TargetBitMap.FromHbitmap(BitMapObject))

Why not just do?

    Dim TargetBitMap As New Bitmap(Original_Image, S.Width, S.Height)
    Dim NewGraphics As Graphics = Graphics.FromImage(TargetBitMap)
    ...
Avatar of bemson
bemson

ASKER

Thanks for replying

The code I had was from another source, not mine.

This is an area I am not familiar with, have jus tried it and works on a file that crashed previously

It is just about midnight here, will check more extensive in about 9 hours, looks good

your help is much appreciated
You're welcome!

In general, you only need GetHbitmap and FromHbitmap when dealing with EXTERNAL APIs and applications that need to pass a Handle to a Bitmap in memory.  From what it looks like, you are only dealing with MANAGED images within the .Net framework.
Avatar of bemson

ASKER

Now that I have had a chance to check this out, as I said original file conversion now works, however with what I think is a 5mp file size 3488 * 2616  4867kb it works with 300 dpi but not 400 or 500

I have "dim s as size "to store image height & width for rectangle
for 300 dpi size is 14533 * 10900 ok
for 400 dpi size is 19378 * 14533 fails on "parameter is not valid" in: dim tagetbitmap as new bitmap("a.jpg",s.width, s.height)
for 500 dpi size is 24222 * 18167 fails on "parameter is not valid" in: dim tagetbitmap as new bitmap("a.jpg",s.width, s.height)

is there a limitation on image size that causes this or did original code try to resolve this?

thanks for your interest



height is dpi * original.height / original.verticalresolution
width is dpi * original.width / original.horizontalresolution

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bemson

ASKER

OK, Thanks

As I am "only the messenger", ie a means to an end for my user who wants this program & I am unfamiliar with the "in depth" meaning of dpi or resolution as applied to this example, it is probable that he does not want more than 300 dpi.

I will check with him tomorrow

However, the file I mentioned I believe now to be 9 megapixels, so even a (say) 12 mp image at 300 dpi will fail also (approx equation to my 9mp at 400 dpi which fails)

If as you say it is related to memory then the message "parameter is not valid" is not valid as it implies the parameters are out of the range specified for the image which is supposed to be an integer value < 32767

so presumably it should be possible (not necessary valid) to code :
dim tagetbitmap as new bitmap("a.jpg",32767, 32767)

anything outside this range would give the message "parameter is not valid" ?

is there anything further you may be able to contribute

again, your help is much appreciated
Avatar of bemson

ASKER

I have some committments that will keep me away for 24 hrs

Regards, Bob
Avatar of bemson

ASKER

If you are able to comment onmy last query it would be appreciated

I again will be away with committments for 24 hrs, if no reply will award points and close question

Thanks for your help so far
Avatar of bemson

ASKER

Your help was much appreciated

Thanks