VB.NET Transparent Image Border - vb.net

I am working with Microsoft Visual Basic 2010 Express and I have ran into an issue with transparent images and picture boxes. When I set the size mode to StretchImage it seems to add a white border around my images. If I leave it set to Normal it displays properly with no added border.
In this picture you can see what I am talking about. The left side is how it's suppose to look. The right side is what happens when I resize the picturebox.
Here is the image I am using
For my program I need to scale these images based on the size input by the user. The input size will never exceed 100x100px. The images are transparent .gif's that are 160x160px and stored in the program resources. These images will only be displayed on screen and never output by the program. I was wondering if there is anyway to scale transparent images without getting a white border around them.

Try manually drawing the image and setting the interpolation mode. You may need to play with different values to get the look you want:
Dim destination = New Bitmap(100, 100)
Dim original = Image.FromFile("gear-256.gif")
Using g = Graphics.FromImage(destination)
g.InterpolationMode = InterpolationMode.HighQualityBilinear
g.DrawImage(original, New Rectangle(0, 0, destination.Width, destination.Height), New Rectangle(0, 0, original.Width, original.Height), GraphicsUnit.Pixel)
End Using
PictureBox1.Image = destination

Related

Batch resize images with different background colors

My goal is to batch resize images with different background colours.
I already used Photoshop to batch process and centre (horizontal and vertical align) 2000 different images into square 200x200 tiles on a white background (the images are different in size, background colour, and sometimes file-type).
I can process and batch resize the images and place them on a white 200x200 tile using the batch processor and recorded 'Actions'. With transparent .png images, this looks great.
The problem is that some of the images have a coloured background. If an image has a coloured background a white border around the image looks quite awful.
How do I dynamically resize the non-transparent images of different colours and sizes to 200x200 square .jpg images? I can do it manually with the colour-picker but is there another option?
As you can see it is only a problem when images have a coloured background.
Thank you in advance!
Save the following script somewhere convenient:
var myDocument = app.activeDocument;
myDocument.colorSamplers.removeAll();
var myColourSampler = myDocument.colorSamplers.add([1 , 1]);
app.backgroundColor = myColourSampler.color;
At the start of the action your going to use for your batch process, record the opening of the script (File >> Scripts >> Browse...), then go and get the script.
It will take the colour of the top left pixel and set it as the background colour, your action should do the rest

Basic picturebox error

I want to add a .gif image to a picturebox.
I want user to choose image from a variety of images of different pixels.
if I use
picturebox1.image = image.fromfile("E:\ship1")
then there is no option available of imagelayout unlike backgroundimage.
because of different images pixel only half of image is displayed in my picturebox size.
I don't want to change my picturebox size because in the game that i am working on, it can cause problems.
If i use
picturebox.backgroundimage = image.fromfile("E:\ship1")
but then .gif would not show its animation.
Any suggestion for solution apart from changing size of picturebox cause that would be the last thing to do

Fit image in picturebox without saving image beforehand?

I have a picture box. The user can draw different rectangles on it, assuming that the user draws rectangles which go beyond the size of the picturebox, can I uniformly resize the contents to fit the picturebox, even though the image is not saved yet?.
I can only manage to resize content that has been loaded in from and already saved image file. The code that I am using to draw rectangles is as follows:
gr.FillRectangle(Brushes.Black, 0, 0, 2, 75)
Rectangles do appear on picturebox as would be expected.
Also I am unable to save using the following code:
PictureBox1.Image.Save("C:\test\myimage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
The error that I get is:
Object reference not set to an instance of an object.
I use System.IO.MemoryStream when I want to fill pictureboxes with images without saving to the hard drive.
PictureBox1.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData("http://upload.wikimedia.org/wikipedia/commons/e/ec/Banksy.on.the.thekla.arp.jpg")))
This is probably because your PictureBox has no "Image" object assigned to it.
You can find the solution here (first answer): getting image from a picturebox
[Also, you might face issues later on with regards to saving to your root folder (C:). Try to use a folder on your local Desktop, for example, to avoid any access-permission errors.]
Edit:
This is taken from the above link. It appears to me that you have not tried it.
myPictureBox.Image = New Bitmap(myPictureBox.ClientSize.Width, _
myPictureBox.ClientSize.Height)
'Then, when you want to perform some drawing, use a Graphics object from the Image instead of the PictureBox:
Using g As Graphics = Graphics.FromImage(myPictureBox.Image)
' do the drawing '
End Using

Picture Box and Form Transparency

Maybe I am missing something, but is it the case that when you set a pictureboxes background to transparent, all it really does is set it to the same color as the forms background?
What I am trying to do is draw an animation for the benefit of this, a bouncing ball - which I paint on the form, then overlay that with a picture frame. End result should be a bouncing ball in a picture frame, I should mention that the picture frame does not have a straight edge, so it is not possible to arrange 4 picture boxes in a frame. The ball needs to vanish behind the frame to change color and then magically bounce back out.
I have tried:
1.Setting the picture box background to pink and then key out the same pink, this basically cuts away everything, including that which is behind the picture box
2.Setting the picture box to transparent, this just displays the picture box background as the same color as the forms background.
3.I have tried painting the image in a rectangle, this had the same effect as drawing it in a picture box.
I am not sure what I am doing wrong, I am wondering if there is any other ways I could try or if someone has made a custom control or library that supports transparency?
Try using a Panel with the background image set. This is because; as you said: the transparent option only takes the backcolour of the parent.
After doing some more in depth research I solved this by drawing both the images to the form using PaintEventArgs and me.paint
Making each image transparent using:
Dim TestImage as Bitmap
TestImage.MakeTransparent(<Transparency Color>)
Thank you to both of you for your replies though.
Hmm... If You want to use Picturebox and get a transparent image,
Try to set picturebox's background as your 'Ball' (probably you need an image editor).
and set picturebox transparent. It worked for me (VS Express 2010) once.

Transparency for images in Visual Basic .net?

I have a picture box on my form, which I add a picture to. This picture has a transparent background, but unfortunately, it seems lost in the picture box... I'm guessing that's because the picture box's background colour property is set to grey (the default). I can't see any option for "transparent" though.
Any idea how I can do it?
Depending on what you are trying to accomplish there's several different ways to go about it.
Some examples are -
Make bitmap transparent
Dim bmp As Bitmap = Bitmap.FromFile("test.bmp")
bmp.MakeTransparent(Color.Magenta) ' magenta in bitmap will be transparent
PictureBox1.Image = bmp
Make the picture box transparent
PictureBox1.BackColor = Color.Transparent
If you really need a transparent image I would suggest not using the picturebox and just render a transparent bitmap directly.
If you want the controls behind the PictureBox to show, ie. you want your image displayed with a transparent background, try drawing straight on the form itself.
Back in the days of VB6 we could do this by hooking onto the Form's "Redraw" event or something...
On most controls in VB, in the Backcolor property of the object, there is an option for transparency. This works fine in VB2008, but in VB2005, you must then set it's .parent property to the object behind (well, in my experience anyways.)
Hope this helps,
I know this is an old topic, but I figured I'd post the solution I came up with for anyone looking for a simple solution for Windows Forms. Say you have PictureBox1 and PictureBox2. You want PictureBox2 to overlay on PictureBox1. Make sure your PictureBox1 background is set to transparent. Then, you can programmatically set:
PictureBox2.Parent = PictureBox1
Magic
Transparent an Image, You can also used Adobe Photoshop to Remove White/Black Background (Watch Tutorial How to Create Transparent Background)
or Use this code below,
PictureBox1.BackColor = Color.Transparent
SetStyle(ControlStyles.SupportsTransparentBackColor, True)