Fit image in picturebox without saving image beforehand? - vb.net

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

Related

Finding a object within a picture and returning its location in VB.NET

I'm trying to accomplish a task of finding a specific object inside a PictureBox in VB.NET and returning the object's location within the PictureBox.
I have the image containing the object inside a PictureBox and a Image saved on my computer at C:\Bitmaps\imgToFind.png which is the object I want to find.
For example, if I have this picture inside the PictureBox:
See Here
and this is the part of the picture I want to find saved at C:\Bitmaps\imgToFind.png:
Part of the Picture
How would I be able to determine the (X,Y) coordinates for the object is located inside the larger picture? (Assuming that the picture is always clear and not altered/misshapen/over layed)?

Excel vba drawing text,lines and polygons on imageor shape object then saving as bitmap or png file

I'm in an excel vba program and I need to display and create a label for a printer.
Now this printer (Zebra Z4M Plus 300 dpi) is extremely fussy and hard to use.
I can now print to it if I create an image file (png or img) of dimensions 944x406 and put my desired label in an area of 797x396 in the upper left corner of that image file.
My question is about creating said image.
Some 10 years ago I remember that it was possible (in probably vb6) to use image primitives such as lines, polygons, circles and text to a picturebox control.
In Excel I have created a form and created an image control however a look at the functions and properties of this object didn't reveal any function for drawing anything on it !
(P.S. I have now found that this image object has a .picture property, this property has a .handle property and .render function, I must be close to something usable but I can't find it. It seems I could load a picture from a picture file using the LoadPicture function on this picture property)
(Perhaps there are windows api function that will let me draw on this .picture using the .picture.handle ?)
The excel Shape object also didn't have drawing primitives but it did have a .CopyPicture function that would allow me to put the contained image in the clipboard (and then I just need to find a way to save the clipboard content to an image file)
If possible I would like to draw to an object which is contained in the worksheet rather than in a userform.
I am looking for further search keywords, names of objects and function for drawing.
Thank you !

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

using a semi-transparent png file in vb.net

I'm trying to paste a png image with a transparency gradient over another image. Problem: the semi-transparent image doesn't show up at all. I have the image embedded as a resource in my project, and I suspect the problem is that VB is importing that image as a bitmap (found the following in resources.designer.vb):
Friend ReadOnly Property alphamask() As System.Drawing.Bitmap
Get
Dim obj As Object = ResourceManager.GetObject("alphamask", resourceCulture)
Return CType(obj, System.Drawing.Bitmap)
End Get
End Property
But so far, I haven't been able to find a workaround for this. The image, when drawn with
G.DrawImage(My.Resources.alphamask, destRect)
in the form_paint event, doesn't show up at all. By the way, this problem is unique with this one, semi-transparent image. All the other images I'm using, in exactly the same way, work perfectly. Any suggestions?

VB.NET Transparent Image Border

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