hope i can get some help here.
For a VB app, I try to place one picturebox over GDI+ drawn image (or another picturebox) in visual studio 2008, and the picturebox includes transparent areas which don't come up transparent.
I have looked through many methods but they don't suit my situation:
a background is drawn using GDI, and for my app I need a picturebox stationary upon it. (the stationary image cant be GDI+ drawn too, because the background updates every while, which causes it to disappear. tried to redraw image also but it makes an ugly flickering effect).
when i try to use picturebox as background, and assign it as parent for the overlaying image for transparency effect, the above image doesn't show at all. here is the VB code:
Dim back As PictureBox
Dim silhouetteAs PictureBox
Dim img As Bitmap = New Bitmap(imgPath)
img.MakeTransparent(Color.White)
back = New PictureBox()
silhouette= New PictureBox()
silhouette.Parent = back
silhouette.Size = New Point(width, height)
silhouette.Location = New Point(xpos, ypos)
silhouette.Image = img
silhouette.BackColor = Color.Transparent
back.Parent = MAIN
back.Size = New Point(width, height)
back.Location = New Point(xpos, ypos)
back.BackColor = Color.Blue
where back is the background and silhouette is the on-top image.
can someone please provide an example of how transparency achieved through parenting pictureboxes? or any other solution to this situation?
Never mind, found out what was wrong - when I assigned another picturebox as a parent, I didn't update the location of the overlaying picturebox relatively. the correction would be to
add:
silhouette.location=new point(0,0)
forgot that the coordinates are relative to the parent and not the form anymore.
Related
I have a Picturebox which I draw a view to (Gantt View in this case) and it works OK - i.e., the view is drawn and the AutoScroll property allows the image in the PictureBox to be smoothly scrolled.
My problem is, the header of the image (e.g., the date headers in this case) scroll off the top of the display when I scroll down the image.
What I can't work out is how to fix a header to the top. I thought about simply drawing a header into another Picturebox, but then I am not sure how to sync the header with the left-right scrolling of the main PictureBox
Can someone suggest the best approach to handling this, or do I need to revert to doing a direct draw and handle the scrolling myself?
I am using VB with VS 2015.
Many thanks
Phil
Updated - I am now using an off-screen Bitmap, but can someone look at the code below and let me know if there is a faster/better way to do this? It all works, but still learning and so always looking to do things the best way
Public Sub MoveViewPoint(G As Graphics)
' G passed in from controls Paint
G.Clear(Color.WhiteSmoke)
' _Plan is off-screen bitmap of image
' _HeaderHeight is height of the Header area in _Plan
Dim Header_src_rect As New Rectangle(_HScroll.Value, 0, _Plan.Width, _HeaderHeight)
Dim Header_dst_rect As New Rectangle(0, 0, _Plan.Width, _HeaderHeight)
G.DrawImage(_Plan, Header_dst_rect, Header_src_rect, GraphicsUnit.Pixel)
Dim src_rect As New Rectangle(_HScroll.Value, _HeaderHeight + 1 + _VScroll.Value, _Plan.Width, _Plan.Height)
Dim dst_rect As New Rectangle(0, _HeaderHeight + 1, _Plan.Width, _Plan.Height)
G.DrawImage(_Plan, dst_rect, src_rect, GraphicsUnit.Pixel)
_HScroll.LargeChange = G.ClipBounds.Width * 0.9
_VScroll.LargeChange = G.ClipBounds.Height * 0.9
End Sub
I would do all your drawing to an off-screen bitmap using the GDI graphics system. You can draw your headers and the rest of the chart as 2 distinct stages in the same bitmap. You would have to handle the scrolling yourself by watching the MouseMove event and checking the buttons status.
I am attempting to overlay two transparent images within a winform, but it keeps rendering the form's background image behind the top transparent image, as opposed to the second image...
My basic set up is I have two panels and in each panel is a picturebox. Each image in the picture boxes have some transparent areas. I've set the BackColor of the panels to color.transparent.
When I make one panel overlay the other I'm seeing the form's backcolor come through as opposed to the underlaying image.
Am I missing a property that I can set?
You only need one picture box. The overlay can be done with graphics.
Imports System.Drawing
Dim OverlayImage As New Bitmap("Some Path", True)
Dim BackImage As New Bitmap("Some Path", True)
g As Graphics = Graphics.FromImage(BackImage)
g.DrawImage(OverlayImage, 0, 0)
pictureBox1.Image = BackImage
If you want to have the timer move the overlayed image, then first, make a variable Dim posX As Integer = 0
then use g.DrawImage(OverlayImage, posX, 0) Now when your timer ticks, increment posX by 10
Here's a complete function to overlay two images (adapted from Blue0500's answer):
''' <summary> Return a new image with one superimposed over the other. </summary>
Function OverlayImgs(ByVal BackgroundImg As System.Drawing.Bitmap, ByVal OverlayImg As System.Drawing.Bitmap, Position As System.Drawing.Point) As System.Drawing.Bitmap
Dim g = System.Drawing.Graphics.FromImage(BackgroundImg)
g.DrawImage(OverlayImg, Position)
Return BackgroundImg
End Function
Usage:
lblTest.Image = OverlayImgs(Img1, Img2, New Point(16, 16))
You don't need a PictureBox for this unless you are using it for the canvas. You can draw all images to a Rectangle structure and move them around. Personally I would create a class object that has a Rectangle, Image and other properties and hold them in a collection. Then you simply draw the class objects by there properties including location. If there images contain transparencies they will overlay for you. This also gives you a method to check for collisions via the Rectangle.IntersectsWith function.
I am creating program with multileyered picturebox, the image of picturebox is update dynamically from bitmap in memory, and i want to clear the selected part on bitmap to transparent color so i can see image of picturebox behind it.
Here is my code
Dim gBmp As Graphics = Graphics.FromImage(GraphLayer(LayerArray))
Dim TileSrcCrop As New Rectangle(nVal(xTile), nVal(yTile), TileSize, TileSize)
Dim TileDrawSize As New Rectangle(nVal(H), nVal(V), TileSize, TileSize)
gBmp.DrawImage(GraphImage(LayerArray), TileDrawSize, TileSrcCrop, GraphicsUnit.Pixel)
PicMap(LayerArray).Image = GraphLayer(LayerArray)
Thanks
What I'm about to say may only be for Windows icons, but IIRC, the top left pixel must be assigned the color that you want to designate as the color for transparency. You'll usually see MS use Magenta (255, 0, 255). This is called a transparency mask. Then, anywhere you want the color to be transparent, you use the color you placed in the top left pixel.
HTH -- and let me know if it's only for icons, but I think it's for Windows bitmap files, too.
I am building a 2D game where the user is a circle(:P) and the enemies are rectangles coming at him. However, my problem is that when I placed a very nice picture of space I found on the internet, the screen draws whatever it has to underneath this image. Everything works,I can still see my lives going down when something collides into me - except the fact it is all covered up by this picture.
In a nutshell, my question is: How Do I Draw everything ON Top of this - (I tried using the 'Send To Back' Command)
EDIT: The form draws everything through a timer, and the user controls his character through keys. This probably won't help - but if it does it's here.
Sorry folks didn't think you'd need the code. Here it is:
In the mybase.load procedure:
PicBackGround.Dock = DockStyle.Fill
PicBackGround is the picture box with the image.
In the paint procedure:
e.Graphics.Clear(Color.Black)
e.Graphics.FillEllipse(Brushes.Orange, Player)
'Projectiles
Dim i As Integer = 0
Do
If Current_Projectile(i).IsEmpty = False Then e.Graphics.FillRectangle(Brushes.Red, Current_Projectile(i))
i += 1
Loop Until i = UBound(Current_Projectile)
'Objects
i = 0
Do
If Objects(i).IsEmpty = False Then e.Graphics.FillRectangle(Brushes.Blue, Objects(i))
i += 1
Loop Until i = UBound(Objects)
Okay: Player is a rectangle declared right at the top, Dim Player As New Rectangle(0, 0, 50, 50);
There is then the array Objects, which stores all the data about the enemies coming at the player, Current_Projectiles is simply an array to store data about rectangles(bullets) that the player fires.
Anything else you want to know just let me know.
Yes, a control overlaps anything you draw on the form. A simple solution is to use the form's BackgroundImage property instead. Or draw the image yourself.
i created a drawing apllication in my solution by this code.by this i can draw images in a picturebox and can to save. when i click the clearbutton the image on the picturebox is cleared but the problem is after clearing the image i can't draw any thing in the picturebox without the form's reload
dim mousepath as new system.drawing.drawing2d.graphicspath()
in pageload
picturebox1.image=new bitmap(picturebox1.clientsize.height,picturebox1.clientsize.width)
picturebox1_paint(...)
myusercolor=(sysytem.drawing.color.black)
myalpha=100
using g as graphics=graphics.fromimage(picturebox1.image)
g.clear(color.white)
dim currentpen as pen=new pen(color.fromargb(myalpha,myusercolor),mypenwidth) g.drawpath(currentpen,mousepath)
in mousedown
if e.button=mousebutton.left then
mousepath.startfigure()
end if
in mousemove
if e.button=mousebutton.left then
mousepath.addline(e.x,e.y,e.x,e.y)
end if
picturebox1.invalidate
clearbutton_click
picturebox1.image.dispose()
picturebox1.image=nothing
PROBLEM:
if anyone knows the solution for this problem please help me.it's very important for me.thank you
when you clear, you set picturebox1.image=nothing, but then your Graphics object comes from that image. That's why it won't work. You have to set a new Bitmap when you clear like the first time :
picturebox1.image=new bitmap(picturebox1.clientsize.height,picturebox1.clientsize.width)
mousepath.Reset();