Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Overlaying images in VB.Net


  • Please log in to reply
12 replies to this topic

#1 aommaster

aommaster

    I !<3 malware


  • Malware Response Team
  • 5,289 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Dubai
  • Local time:10:59 AM

Posted 26 December 2009 - 09:43 PM

Hi!

I have two PNG files that contain some transparent areas. Both files have exactly the same dimensions. I'd like to overlay one on the other, and then output the result to a picturebox.

So far, I have:
Dim img As New Bitmap(46, 55)
		Dim g As Graphics = Graphics.FromImage(img)
		g.DrawImage(My.Resources.bgblue, 0, 0)
		g.DrawImage(My.Resources.tree, 0, 0)

		img = New Bitmap(img.Width, img.Height, g)
		img.Save("C:\Test1.png")

However, my output file comes out to be a filled black square.

I don't really have experience with image manipulation in programming, so I'm not really sure what I've done wrong.

Any advice would be greatly appreciated :thumbsup:

Edit:
These images are all stored in a resource file. I'd like to have the user type in "tree" and the file stored in My.Resources.tree would be pulled up. How would I go about doing that? I can't use string concatenation, because, well, it's a string.

Any ideas?

Edited by aommaster, 26 December 2009 - 11:15 PM.

My website: http://aommaster.com
unite_blue.png
Please do not send me PM's requesting for help. The forums are there for a reason : )
If I am helping you and do not respond to your thread for 48 hours, please send me a PM


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:59 AM

Posted 27 December 2009 - 12:16 AM

:thumbsup:

I can't use string concatenation, because, well, it's a string.

What would you use it for then?

#3 aommaster

aommaster

    I !<3 malware

  • Topic Starter

  • Malware Response Team
  • 5,289 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Dubai
  • Local time:10:59 AM

Posted 27 December 2009 - 12:21 AM

Oh, that was a minor typo on my part :thumbsup: I meant to say it was not a string.

With regards to my first question about overlays, I managed to do it. For completeness, here's how I did it:
bg = New Bitmap(My.Resources.blue)
g = Graphics.FromImage(bg)

overlay = New Bitmap(tree.Image)
g.DrawImage(overlay, 0, 0)
finalpic.Image = bg

Now, I'm still stuck with how to access resources from string names.

I'd ideally like to have something like this:
path= "my.resources." + inputbox.text
overlay = New Bitmap(path)
(The above is where I attempted to use a string concatenation operator)

However, this doesn't work because the path, albeit a string, is not supposed to be string, but a piece of code that tells the function to access the resources file.

Hopefully that makes a bit of sense?

My website: http://aommaster.com
unite_blue.png
Please do not send me PM's requesting for help. The forums are there for a reason : )
If I am helping you and do not respond to your thread for 48 hours, please send me a PM


#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:59 PM

Posted 27 December 2009 - 01:34 AM

Hello, aommaster :thumbsup:

However, this doesn't work because the path, albeit a string, is not supposed to be string, but a piece of code that tells the function to access the resources file.

Hopefully that makes a bit of sense?

You won't be able to do this directly because your user would need access to a compiler to do that. VB.NET is compiled, not interpreted ;). You can, however, access a resource by name. Everything in the "My" namepspace is syntactic sugar applied by the VB.NET compiler for you. You can simply use the C# method (or more generally, the "raw" .NET method) of getting at the resources if you'd like to use a name. The docs for the System.Resources Namespace should get you started :flowers:

Hope that helps,
Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 aommaster

aommaster

    I !<3 malware

  • Topic Starter

  • Malware Response Team
  • 5,289 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Dubai
  • Local time:10:59 AM

Posted 27 December 2009 - 04:29 AM

Hi Billy!

Thanks for your reply!

So, it seems to be working fine, just wondering whether it's good practice to do it the way I did:
Dim Assembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly
Dim path As String = Assembly.GetName().Name.ToString() + "." + inputbox.text + ".png"
Dim stream As IO.Stream = Nothing

stream = Assembly.GetManifestResourceStream(path)
overlay = New Bitmap(stream)
And this correctly reads the file in question.

However, I seemed to have encountered a problem with the overlay. So far, my overlay function looks like:
bg = New Bitmap(My.Resources.blue)
g = Graphics.FromImage(bg)

overlay = New Bitmap(tree.Image)
g.DrawImage(overlay, 0, 0)

overlay = New Bitmap(stream)
g.DrawImage(overlay, 0, 0)

finalpic.Image = bg
However, it looks like the last overlay (that one reading from the stream) doesn't seem to work. What's the reason for this?

My website: http://aommaster.com
unite_blue.png
Please do not send me PM's requesting for help. The forums are there for a reason : )
If I am helping you and do not respond to your thread for 48 hours, please send me a PM


#6 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:59 PM

Posted 27 December 2009 - 10:52 AM

Unfortunately, I don't know. I've never used the graphics handling functions :thumbsup:

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#7 aommaster

aommaster

    I !<3 malware

  • Topic Starter

  • Malware Response Team
  • 5,289 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Dubai
  • Local time:10:59 AM

Posted 27 December 2009 - 09:21 PM

Hi Billy!

Thanks for trying! For what it's worth, turns out the the overlay function works fine. However, it seems that if I read the image from the stream and try to overlay that, the image that is being overlayed is somehow larger than it's true size, ie, somewhere along the way, it gets resized larger, and *then* overlayed.

Not too sure what's going on with that...

My website: http://aommaster.com
unite_blue.png
Please do not send me PM's requesting for help. The forums are there for a reason : )
If I am helping you and do not respond to your thread for 48 hours, please send me a PM


#8 aommaster

aommaster

    I !<3 malware

  • Topic Starter

  • Malware Response Team
  • 5,289 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Dubai
  • Local time:10:59 AM

Posted 11 January 2010 - 09:52 AM

For what it's worth, I posted a question up on the MSDN here and I got a reply.

In simple:
Internally, VB .NET handles bitmaps from file streams and bitmaps from the resource file slightly differently, and that caused the problem. In the fact, it turns out that the overlay did work, but it overlays an enlarged image (turns out my image was mostly transparent, and hence, appeared to not be overlayed).

The solution:
Overlay all bitmaps using a file stream instead, rather than call one from the resource file.

That seemed to fix my problem

My website: http://aommaster.com
unite_blue.png
Please do not send me PM's requesting for help. The forums are there for a reason : )
If I am helping you and do not respond to your thread for 48 hours, please send me a PM


#9 A30N

A30N

  • Members
  • 6 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:05:59 PM

Posted 11 July 2010 - 08:14 PM

Hey, sorry for reviving this thread.

thanks to all the guys above, because ive been having my own problems regarding a very simmilar issue :/
and thats all pretty much solved now, but im still having one really annoying error that you guys might be able to help me out with :thumbsup:

[codebox]'=========CHARACTER CREATION========
Dim counter = 3
Dim clothes(6) As String

For counter = 3 To 9
Dim path As String = mydetails(counter)
path = "My.Resources." & path
clothes(counter - 3) = path
Next

'=====CHARACTER IMAGE CREATION=====
Dim bg = New Bitmap(My.Resources.Blueshield)
Dim g = Graphics.FromImage(bg)


Dim overlay0 = New Bitmap(clothes(0))
Dim overlay1 = New Bitmap(clothes(1))
Dim overlay2 = New Bitmap(clothes(2))
Dim overlay3 = New Bitmap(clothes(3))
Dim overlay4 = New Bitmap(clothes(4))
Dim overlay5 = New Bitmap(clothes(5))
Dim overlay6 = New Bitmap(clothes(6))

g.DrawImage(overlay0, 0, 0)
g.DrawImage(overlay1, 0, 0)
g.DrawImage(overlay2, 0, 0)
g.DrawImage(overlay3, 0, 0)
g.DrawImage(overlay4, 0, 0)
g.DrawImage(overlay5, 0, 0)
g.DrawImage(overlay6, 0, 0)

PictureBox1.Image = bg[/codebox]

ok, so basically ive got an array (mydetails) with a bunch of names that relate to images in my resources file. so im reading them into a new array, but adding "my.resources." to the string so that they can be used. but when i hit the line for the first overlay, i get an error "Parameter is not valid" is this because its somehow a command and they arent recognized when part of a string? and if so, does anyone know how to get around this?

cheers :flowers:

oh p.s. in case you were wondering, its for a game im doing for Software Design at school :trumpet:

Edited by A30N, 11 July 2010 - 08:19 PM.

KIRBY <3's YOU :D

(>*.*)> <(*.*<)

#10 aommaster

aommaster

    I !<3 malware

  • Topic Starter

  • Malware Response Team
  • 5,289 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Dubai
  • Local time:10:59 AM

Posted 11 July 2010 - 08:28 PM

Hi!

I'm not much of a pro, but I believe you're mixing up two methods of accessing resources. The first way is using the resource manager (my.resources). The second way is using the GetManifest resource stream.

So, in your case, you'd want to first get the name of the assembly, and then append the name of the image (I'm assuming for mydetails array contains the names of images):
[codebox]
Dim counter = 3
Dim clothes(6) As String
Dim Assembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly
Dim path As String = ""
Dim stream As IO.Stream = Nothing

For counter = 3 To 9
Dim path As String = mydetails(counter)
path = Assembly.GetName().Name.ToString() + "." + mydetails(counter)
stream = Assembly.GetManifestResourceStream(path)
Next
[/codebox]
(Mind you, I haven't tested the code out, but it should put you on the right track)

My website: http://aommaster.com
unite_blue.png
Please do not send me PM's requesting for help. The forums are there for a reason : )
If I am helping you and do not respond to your thread for 48 hours, please send me a PM


#11 A30N

A30N

  • Members
  • 6 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:05:59 PM

Posted 11 July 2010 - 08:44 PM

Hey! Thanks for your reply!

yeah thanks for that, but im a little confused.
what is the line:
[codebox]stream = Assembly.GetManifestResourceStream(path)[/codebox]
used for? because stream retains its value of nothing.
so at the end it seems asthough its unused. also, when i add
[codebox]clothes(counter - 3) = path[/codebox]
to the end, then the overlay comes into play, i still get the same error

sorry for being such a pain about this :thumbsup:
KIRBY <3's YOU :D

(>*.*)> <(*.*<)

#12 aommaster

aommaster

    I !<3 malware

  • Topic Starter

  • Malware Response Team
  • 5,289 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Dubai
  • Local time:10:59 AM

Posted 11 July 2010 - 09:09 PM

Hi!
Again, I'm no expert on this (I'm, in fact, very new to all of this), so I may be mistaken. Here's how I understand it...

This line:
stream = Assembly.GetManifestResourceStream(path)
Takes a given string which corresponds to the address of the file in the resource manifest and opens up a stream so that you can read bytes from it.

because stream retains its value of nothing.

This may be a clue as to why you're getting your "parameter is not valid" error. If stream retains its value of nothing, then it means that the resource could not be accessed (which I'm pretty sure would cause problems later down in your program).

Here's something you could try:
Add a breakpoint to the loop and take a look at the value of path. It should be something along the lines of programname.imagename.extension

Hope that helps :thumbsup:

My website: http://aommaster.com
unite_blue.png
Please do not send me PM's requesting for help. The forums are there for a reason : )
If I am helping you and do not respond to your thread for 48 hours, please send me a PM


#13 A30N

A30N

  • Members
  • 6 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:05:59 PM

Posted 31 July 2010 - 09:17 PM

Hey thanks for all the help, but ive looked everywhere and tried heaps of different ways, and i just cant seem to get it working... instead ive resorted to some ugly long select cases instead :/
really appreciate your help though, cheers. ill definitely come back here if ive got any more problems :thumbsup:
KIRBY <3's YOU :D

(>*.*)> <(*.*<)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users