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)
Visual Basic.NET

Avatar of undefined
Last Comment
bemson

8/22/2022 - Mon
Mike Tomlinson

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)
    ...
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
Mike Tomlinson

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
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
Mike Tomlinson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
bemson

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

Regards, Bob
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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
bemson

ASKER
Your help was much appreciated

Thanks