Picture Shaped not working - vb.net

Th following code is not working when the Picture shaped form.
Dim Img As New System.Drawing.Bitmap(My.Resources.imgpng)'ImgPng is a resource Image
' The color at Pixel(1,1) is rendered as transparent for the complete background.
Img.MakeTransparent(Img.GetPixel(1, 1))
Me.BackgroundImage = Img
Me.TransparencyKey = Img.GetPixel(1, 1)
Can anybody Help me to get more closer?

I set the form background to the same via this code and it worked:
Dim Img As New System.Drawing.Bitmap(My.Resources.imgpng)'ImgPng is a resource Image
' The color at Pixel(1,1) is rendered as transparent for the complete background.
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Dim Color = Img.GetPixel(0, 0)
Me.BackColor = Color
Img.MakeTransparent(Color)
Me.TransparencyKey = Color
Me.BackgroundImage = Img
And I used (0,0) for the transparency pixel though that should not matter unless your (1,1) was the wrong color.

Thanks man...
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Img As New System.Drawing.Bitmap(My.Resources.NewImage) 'NewImage is a resource Image
' The color at Pixel(1,1) is rendered as transparent for the complete background.
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Dim Color = Img.GetPixel(0, 0)
Me.BackgroundImageLayout = ImageLayout.Stretch ' To Adjust the Image
Me.BackColor = Drawing.Color.Black
Img.MakeTransparent(Drawing.Color.Black)
Me.TransparencyKey = Drawing.Color.Black
Me.BackgroundImage = Img
End Sub
For a change 'Img.MakeTransparent(Color)' is not required. Try to getva decent image with outlined border to eliminate bad edges...

Related

Changing color, highlight, background size of a ListBox?

So I've searched around and managed to highlight the values in my ListBox, but I'm trying to increase the font size for the text inside the ListBox but it results in the following image as attached:
This is my current code:
Private Sub ListBox1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
If ListBox1.Items(e.Index).ToString().Contains("*") Then
e.Graphics.FillRectangle(Brushes.Red, e.Bounds)
End If
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, Brushes.Black, New System.Drawing.PointF(e.Bounds.X, e.Bounds.Y))
e.DrawFocusRectangle()
I've tried messing with the "e.Bounds.X, e.Bounds.Y" to increase the rectangle/size of the highlighted value but nothing seemed to work.
How does one allow the highlighted rectangle increase according to the font size?
When you set the DrawMode of a ListBox control to OwnerDrawVariable or you change the Font size after the control handle has been created (i.e. after it has already processed the WM_MEASUREITEM message), you need to manually set the ItemHeigh property to the new Font height.
The ItemHeight property is set subscribing the ListBox MeasureItem event and setting the MeasureItemEventArgs e.ItemHeight property.
Also, if you change the Font size on the fly, you also need to force the WM_MEASUREITEM message to be re-sent to the ListBox control, otherwise the Items Bounds will not be updated.
In other words, when the DrawItem event is raised, the DrawItemEventArgs e.Bounds property will report wrong measures.
A way to force the ListBox control to re-measure its Items bounds, is to set ListBox.DrawMode = DrawMode.Normal and immediatly resetting it back to OwnerDrawVariable. This causes the WM_MEASUREITEM message to be processed again.
listBox1.DrawMode = DrawMode.Normal
listBox1.DrawMode = DrawMode.OwnerDrawVariable
listBox1.Update()
Here I'm using the Font.Height to measure the current ItemHeight in the MeasureItem event, because it rounds up the measure. You could use TextRenderer.MeasureText or Font.GetHeight(); you'll end up with the same measure, but rounded down.
Private Sub ListBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem
Dim ItemForeColor As Color
Dim ItemBackColor As Color
e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
ItemForeColor = Color.FromKnownColor(KnownColor.HighlightText)
ItemBackColor = If(ListBox1.Items(e.Index).ToString().Contains("*"), Color.Red, Color.FromKnownColor(KnownColor.Highlight))
Else
ItemForeColor = ListBox1.ForeColor
ItemBackColor = If(ListBox1.Items(e.Index).ToString().Contains("*"), Color.Red, ListBox1.BackColor)
End If
Using TextBrush As New SolidBrush(ItemForeColor)
Using ItemBrush As New SolidBrush(ItemBackColor)
e.Graphics.FillRectangle(ItemBrush, e.Bounds)
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), ListBox1.Font, TextBrush, e.Bounds, StringFormat.GenericTypographic)
End Using
End Using
e.DrawFocusRectangle()
End Sub
Private Sub ListBox1_MeasureItem(sender As Object, e As MeasureItemEventArgs) Handles ListBox1.MeasureItem
e.ItemHeight = ListBox1.Font.Height
End Sub
Test it resizing the Font:
ListBox1.Font = New Font(ListBox1.Font.FontFamily, ListBox1.Font.SizeInPoints + 2, ListBox1.Font.Style, GraphicsUnit.Point)
ListBox1.DrawMode = DrawMode.Normal
ListBox1.DrawMode = DrawMode.OwnerDrawVariable
ListBox1.Height = '[OriginalHeight]
ListBox1.Update()

Saving the image of a picturebox

So I have this code:
Private Sub button28_Click(sender As Object, e As EventArgs) Handles button28.Click
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = PicOuterBorder.Bounds
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
picFinal.Image = screenshot
'this takes a screenshot
End Sub
PicOuterBorder is a picturebox on my form. PicFinal is another display picturebox. But this code gets me this: Which is basically a screenshot of a window in the size of PicOuterBorder starting from the origin of my screen. However, Me.Bounds instead of PicOuterBorder.Bounds works and gets a perefect screenshot of just my form. I want picFinal to have a screenshot of just PicOuterBorder
Try below code. You have to map the control coordinates to screen coordinates using PointToScreen. I have placed PicOuterBorder inside the panel PanelPicture. PanelPicture is without any border, while PicOuterBorder can have any type of border style. Below code takes the snapshot of the panel.
Private Sub button28_Click(sender As Object, e As EventArgs) Handles button28.Click
Dim graph As Graphics = Nothing
Dim bounds As Rectangle = Nothing
Dim screenshot As System.Drawing.Bitmap
Dim location As Drawing.Point = PanelPicture.PointToScreen(Drawing.Point.Empty)
screenshot = New System.Drawing.Bitmap(PanelPicture.Width, PanelPicture.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(location.X, location.Y, 0, 0, PanelPicture.Size, CopyPixelOperation.SourceCopy)
picFinal.Image = screenshot
graph.Dispose()
End Sub
Adapt your code for something like this:
Public Sub SaveImage(filename As String, image As Image, Encoder As ImageCodecInfo, EncParam As EncoderParameter)
Dim path As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, filename & ".jpg")
Dim mySource As New Bitmap(image.Width, image.Height)
Dim grfx As Graphics = Graphics.FromImage(mySource)
grfx.DrawImageUnscaled(image, Point.Empty)
grfx.Dispose()
mySource.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg)
mySource.Dispose()
End Sub

Making a color picker/eye dropper tool for drawing program in visual basic?

I have a drawing program I am developing in VB. In the mousedownevent while a certain radiobutton is ticked, I want the mousedownevent handler to grab the color of the selected pixel that is clicked in a picturebox with an image. How do I do that? Thanks.
If you create a Bitmap object and assign it to your PictureBox's image you can use the GetPixel method to get a pixel's color from the specified point.
Dim PickedColor As Color
Private Sub PictureBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
PickedColor = DirectCast(PictureBox1.Image, Bitmap).GetPixel(e.X, e.Y)
End Sub
Granted, I had interpolation mode set because this is a pixel editor and needed basic coloring for video game 8 bit design and antialiasing is not what i needed so this fixed it.
Dim COLOR1 as color
Dim x As Integer = 0
Dim y As Integer = 0
Dim Image1 As New Bitmap(picturebox1.Image, picturebox1.Width, picturebox1.Height)
Using g As Graphics = Graphics.FromImage(Image1)
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half
g.DrawImage(BackgroundImage1, 0, 0, Image1.Width, Image1.Height)
End Using
For x = 0 To picturebox1.Width
Next
For y = 0 To picturebox1.Height
Next
COLOR1 = New Bitmap(Image1).GetPixel(e.X, e.Y)
ColorDialog2.Color = COLOR1
PictureBox3.BackColor = COLOR1
But if you want this color picker without the interpolation pixel mode, so you can color pick on jpegs and whatnot, just take out the whole using code. Works fine.

Button images, transparency, MouseOver colors

I have a button that I have made Flat, with no border so when there is an image in it, it is seamless looking.
The image I want to add to this button is basically just an unfilled rectangle (png from a file).
When I mouse over this button, I have the backcolor change to red. When it does, the rectangle that is the image obviously does not change to red..it stays whatever color it is from the image.
I wanted to know if there is a way to add an image, who's actual backcolor is transparent (I suppose?).
The image is a blue rectangle with a very very light brown background. When I mouse over I would like the ENTIRE button's backcolor to be red, but only maintain the blue rectangle. Right now when you mouseover you can clearly see the size of the image itself.
I'd rather not draw graphics to the button. (In actuality this image is an outline of a computer monitor...but it's essentially a rectangle for this case)
Is there a way to do this with the Image or BackgroundImage properties?
Put Your image in Resources (Project/Properties/Resources/Images/Add Resource and from dropdown menu choose Add Existing File...).
Then use this code :
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Button1
.Text = ""
.UseVisualStyleBackColor = False
.BackColor = Color.Transparent
.FlatStyle = FlatStyle.Flat
.FlatAppearance.BorderSize = 0
.FlatAppearance.MouseOverBackColor = Color.Red
.BackgroundImageLayout = ImageLayout.Center
.BackgroundImage = My.Resources.XXX 'image name from Resources
End With
End Sub
Private Sub Button1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseHover
With Button1
.BackgroundImage = Nothing
.FlatAppearance.BorderColor = Color.Blue
.FlatAppearance.BorderSize = 2 'or what is size of Your border in px (take from Your image)
End With
End Sub
Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
With Button1
.BackColor = Color.Transparent
.BackgroundImage = My.Resources.XXX 'image name from Resources
.FlatAppearance.BorderSize = 0
End With
End Sub
Of course, You can create Your own button (class) and import into Your project, where You can avoid this MouseHover and MouseLeave for every button.
btw. All this You can do without image if Your picture have only border and background of image isn't gradient. Then You can avoid MouseHover and MouseLeave.
Edit :
There is code if You don't need image (border of button is always blue of 2px, background color is some kind of light brown, and on hover background color is red but border stay blue with 2px).
With Button2
.UseVisualStyleBackColor = False
.BackColor = Color.FromArgb(217, 195, 154)
.FlatStyle = FlatStyle.Flat
.FlatAppearance.BorderSize = 2
.FlatAppearance.BorderColor = Color.Blue
.FlatAppearance.MouseOverBackColor = Color.Red
End With

VB.Net Background gradient using LinearGradientBrush and display Image

I try to achieve to paint a form with a gradient backcolor and overlap an image with transparency.
This is possible?
I want using a tile background image with transparent background and paint the background with a custom linear gradient.
I do it!, I want share my solution with you (It's pretty easy):
External help: Tile a Shape with an Image
Private Sub BackgroundGradient(ByRef Control As Object, _
ByVal Color1 As Drawing.Color, _
ByVal Color2 As Drawing.Color)
Dim vLinearGradient As Drawing.Drawing2D.LinearGradientBrush = _
New Drawing.Drawing2D.LinearGradientBrush(New Drawing.Point(Control.Width, Control.Height), _
New Drawing.Point(Control.Width, 0), _
Color1, _
Color2)
Dim vGraphic As Drawing.Graphics = Control.CreateGraphics
' To tile the image background - Using the same image background of the image
Dim vTexture As New Drawing.TextureBrush(Control.BackgroundImage)
vGraphic.FillRectangle(vLinearGradient, Control.DisplayRectangle)
vGraphic.FillRectangle(vTexture, Control.DisplayRectangle)
vGraphic.Dispose() : vGraphic = Nothing : vTexture.Dispose() : vTexture = Nothing
End Sub
Here's how to draw the gradient background. Tiling the image will be slow unless you use the windows API or something.
Imports System.Drawing
Public Class frmBG
Private Sub frmBG_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
Dim p1 As Point = Me.ClientRectangle.Location
Dim p2 As Point = New Point(Me.ClientRectangle.Right, Me.ClientRectangle.Bottom)
Using brsGradient As New System.Drawing.Drawing2D.LinearGradientBrush(p1, p2, Color.Red, Color.Blue)
g.FillRectangle(brsGradient, e.ClipRectangle)
g.DrawImage(My.Resources.demoImage, Me.ClientRectangle.Location)
End Using
End Sub
Private Sub frmBG_ResizeEnd(sender As Object, e As System.EventArgs) Handles Me.ResizeEnd
Me.Invalidate()
End Sub
End Class