Display Camera Image in VS PictureBox W/0 Saving To PC Drive - vb.net

If I save (to disk) the image transferred from the camera, I can display it in a PictureBox, as follows:
Dim picNum As Integer = 1
Dim imgName As String
Dim imgExt As String
Dim WiaDialog1 As New WIA.CommonDialog
Dim WiaDevice1 As WIA.Device = WiaDialog1.ShowSelectDevice(WIA.WiaDeviceType.CameraDeviceType, False, False)
imgName = WiaDevice1.Items(picNum).Properties("Item Name").Value
imgExt = "." & WiaDevice1.Items(picNum).Properties("Filename extension").Value
Dim image1 As WIA.ImageFile = WiaDevice1.Items(picNum).Transfer(WIA.FormatID.wiaFormatPNG)
image1.SaveFile("C:\PhotoSort\CameraNew2\" & imgName & imgExt)
image1 = "C:\PhotoSort\CameraNew2\" & imgName & imgExt"
PictureBox1.Image = image1
I want to display the transferred image, in a similar VB code manner,. without saving it to disk

You can display image from physical location like
Picturebox1.Image=Image.FromFile("Path");

Related

How do I prevent overwriting the name of a saved image, if the previously saved image was deleted?

Dim data As IDataObject
Dim bmap As Image
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
data = Clipboard.GetDataObject
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
PictureBox1.Image = bmap
SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0)
DestroyWindow(hHwnd)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim counter = My.Computer.FileSystem.GetFiles("C:\Data\Image")
Dim intCount As Integer
Dim strfilename As String
intCount = CStr(counter.Count)
strfilename = "Image" & intCount + 1
PictureBox1.Image.Save("C:\Data\Image\" & strfilename & ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
Dim strpath As String = "C:\Data\Image\" & strfilename & ".jpeg"
Label1.Text = strpath
End If
the name of the image that the image is stored in. image1, image2, image3, image4, image5, image6, image7, image8 and so on. if any image with the name image4 is deleted then the image file to be saved will look for the name with the largest number and then add 1
A better way to do this is to go to "My Project" => "Settings" and create "intCounter" as an integer. (Do not change the value)
https://i.stack.imgur.com/1iGyI.png
Every time an image is saved the intCounter should increase by 1. This can be done by: Remote.My.Settings.intCounter = Remote.My.Settings.intCounter + 1
strfilename = "Image" & Remote.My.Settings.intCounter
If the program is closed, the value of intCounter will be kept.

Preserve Exif/Image Properties

In a web app, users can upload images, which save on the server. It creates thumbnails of the image on the back end with the following code, but all exif data of the image is lost.
I want to preserve the exif data from the original image all throughout the process, and have it included in the final stream. I can read the exif properties from the image, but can't get it to preserve when saving.
To clarify, I am not trying to read, parse, or do anything specific to the metadata itself, I just want to preserve it exactly as it was in the original image during image processing.
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'prepare image
Dim imgFullSize As System.Drawing.Image
'attempt to get the image
Dim sInputURL As String = HttpUtility.UrlDecode(Request.QueryString("IptFl"))
If InStr(sInputURL, "http://") > 0 Or InStr(sInputURL, "https://") Then
'from web url
Dim imgWebRequest As System.Net.WebRequest
Dim imgWebResponse As System.Net.WebResponse
imgWebRequest = System.Net.WebRequest.Create(sInputURL)
imgWebResponse = imgWebRequest.GetResponse()
imgFullSize = System.Drawing.Image.FromStream(imgWebResponse.GetResponseStream())
Else
'from file
imgFullSize = System.Drawing.Image.FromFile(Server.MapPath(sInputURL))
End If
' Dim props As PropertyItem() = imgFullSize.PropertyItems
' For Each prop As PropertyItem In props
' Response.Write(prop.Id.ToString() & "<br />")
' Next
'determine type
Dim sFileExtension As String = "png"
If Left(Right(sInputURL, 4), 1) = "." Then sFileExtension = Right(sInputURL, 3) 'for jpg, gif, etc
If Left(Right(sInputURL, 5), 1) = "." Then sFileExtension = Right(sInputURL, 4) 'for tiff, jpeg, etc
Dim jpgEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
Dim gifEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Gif)
Dim pngEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Png)
'set the quality
Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
Dim myEncoderParams As New EncoderParameters(1)
Dim myEncoderQuality As New EncoderParameter(myEncoder, CType(100L, Int32))
myEncoderParams.Param(0) = myEncoderQuality
'save img to memory stream
Dim ms As New MemoryStream()
Select Case sFileExtension
Case "jpg" : imgFullSize.Save(ms, jpgEncoder, myEncoderParams)
Case "png", "gif" : imgFullSize.Save(ms, pngEncoder, myEncoderParams)
Case Else : imgFullSize.Save(ms, pngEncoder, myEncoderParams)
End Select
'output the memory stream
Response.ContentType = "image/" & sFileExtension
ms.WriteTo(Response.OutputStream)
'---> when I save the above image and look at the exif, it doesn't exist
End Sub
Private Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo
Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageDecoders()
Dim codec As ImageCodecInfo
For Each codec In codecs
If codec.FormatID = format.Guid Then Return codec
Next codec
Return Nothing
End Function
So this did the trick. Basically I had to loop through the source image properties, and for each, set the property in the new image.
'retrieve relative path to image
Dim sSrc As String = HttpUtility.UrlDecode(Request.QueryString("IptFl"))
'prepare image
Dim oImg As System.Drawing.Image
'attempt to get the image, either from web or local file
If InStr(sSrc, "http://") > 0 Or InStr(sSrc, "https://") Then
Dim oRequest As System.Net.WebRequest = System.Net.WebRequest.Create(sSrc)
Dim oResponse As System.Net.WebResponse = oRequest.GetResponse()
oImg = System.Drawing.Image.FromStream(oResponse.GetResponseStream())
oResponse.Close()
Else
oImg = System.Drawing.Image.FromFile(Server.MapPath(sSrc))
End If
'discard if image file not found
If oImg Is Nothing Then Response.End()
Dim sFileExt As String = Split(sSrc, ".")(UBound(Split(sSrc, ".")))
Dim oCanvas As System.Drawing.Bitmap = New System.Drawing.Bitmap(oImg.Width, oImg.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555)
For Each pi As PropertyItem In oImg.PropertyItems
oCanvas.SetPropertyItem(pi)
Next
Dim oGraphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(oCanvas)
oGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default
oGraphic.DrawImage(oImg, 0, 0, oImg.Width, oImg.Height)
Dim oStream As System.IO.MemoryStream = New System.IO.MemoryStream()
oCanvas.Save(oStream, System.Drawing.Imaging.ImageFormat.Jpeg)
Response.ContentType = "image/" & sFileExt
'output the memory stream
oStream.WriteTo(Response.OutputStream)
Response.End()

VB.NET - Take a screenshot of all screens on a computer

I'm trying to capture any and all screens on a computer, I tried to fiddle with Screen.AllScreens and also something with VirtualScreens that I can't remember, so I moved to PrimaryScreen to make sure everything else worked properly.
Here is my current class:
Public Class wmCapture
Public Shared Function screenCapture()
Dim userName As String = Environment.UserName
Dim savePath As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim dateString As String = Date.Now.ToString("yyyyMMddHHmmss")
Dim captureSavePath As String = String.Format("{0}\WM\{1}\capture_{2}.png", savePath, userName, dateString)
Dim bmp As Bitmap = New Bitmap( _
Screen.PrimaryScreen.Bounds.Width, _
Screen.PrimaryScreen.Bounds.Height)
Dim gfx As Graphics = Graphics.FromImage(bmp)
gfx.CopyFromScreen( _
Screen.PrimaryScreen.Bounds.Location, _
New Point(0, 0), Screen.PrimaryScreen.Bounds.Size)
bmp.Save(captureSavePath)
End Function
End Class
What should I be using in the Screen namespace to include all active screens?
You were close. I made a couple adjustments and can confirm it's working on my end.
Public Shared Sub screenCapture()
Dim userName As String = Environment.UserName
Dim savePath As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim dateString As String = Date.Now.ToString("yyyyMMddHHmmss")
Dim captureSavePath As String = String.Format("{0}\WM\{1}\capture_{2}.png", savePath, userName, dateString)
' This line is modified for multiple screens, also takes into account different screen size (if any)
Dim bmp As Bitmap = New Bitmap( _
Screen.AllScreens.Sum(Function(s As Screen) s.Bounds.Width),
Screen.AllScreens.Max(Function(s As Screen) s.Bounds.Height))
Dim gfx As Graphics = Graphics.FromImage(bmp)
' This line is modified to take everything based on the size of the bitmap
gfx.CopyFromScreen(SystemInformation.VirtualScreen.X,
SystemInformation.VirtualScreen.Y,
0, 0, SystemInformation.VirtualScreen.Size)
' Oh, create the directory if it doesn't exist
Directory.CreateDirectory(Path.GetDirectoryName(captureSavePath))
bmp.Save(captureSavePath)
End Sub

Displaying image from folder/file in vb.net

Dim ImagePath As String = "images/spaceship2.png"
Dim img1 As Bitmap
Dim newImage As Image = Image.FromFile("images/spaceship2.png")
img1 = New Bitmap(ImagePath)
pb2.ImageLocation = ImagePath
pb1.Image = newImage
I want to display image from folder, for example, student with an id number of 22137471, the picture with the name of 22137471 will be display on my picture box, between, i saw this code somewhere in google.
i want to display image from folder, for example, student with an id
number of 22137471, the picture with the name of 22137471 will be
display on my picture box
Try something like...
Dim id As String = "22137471"
Dim folder As String = "c:\some path\folder"
Dim filename As String = System.IO.Path.Combine(folder, id & ".png")
PictureBox1.Image = Image.FromFile(filename)
Here's an updated version that doesn't lock the original image file:
Dim id As String = "22137471"
Dim folder As String = "c:\some path\folder"
Dim filename As String = System.IO.Path.Combine(folder, id & ".png")
Try
Using fs As New System.IO.FileStream(filename, IO.FileMode.Open)
PictureBox1.Image = New Bitmap(Image.FromStream(fs))
End Using
Catch ex As Exception
Dim msg As String = "Filename: " & filename &
Environment.NewLine & Environment.NewLine &
"Exception: " & ex.ToString
MessageBox.Show(msg, "Error Opening Image File")
End Try
You can try this:
PictureBox1.Image = Image.FromFile("c:\some path\folder\myImage.jpg")

How to Display a Bmp in a RTF control in VB.net

I Started with this C# Question
I'm trying to Display a bmp image inside a rtf Box for a Bot program I'm making.
This function is supposed to convert a bitmap to rtf code whis is inserted to another rtf formatter srtring with additional text. Kind of like Smilies being used in a chat program.
For some reason the output of this function gets rejected by the RTF Box and Vanishes completly. I'm not sure if it the way I'm converting the bmp to a Binary string or if its tied in with the header tags
lb.SelectedRtf = FormatText(build.ToString, newColor)
'returns the RTF string representation of our picture
Public Shared Function PictureToRTF(ByVal Bmp As Bitmap) As String
'Create a new bitmap
Dim BmpNew As New Bitmap(Bmp.Width, Bmp.Height, Imaging.PixelFormat.Format24bppRgb)
Dim gr = Graphics.FromImage(BmpNew)
gr.DrawimageUnscaled(Bmp, 0, 0)
gr.dispose()
Dim stream As New MemoryStream()
BmpNew.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp)
Dim bytes As Byte() = stream.ToArray()
Dim str As String = BitConverter.ToString(bytes, 0).Replace("-", String.Empty)
'header to string we want to insert
Using g As Graphics = Main.CreateGraphics()
xDpi = g.DpiX
yDpi = g.DpiY
End Using
Dim _rtf As New StringBuilder()
' Calculate the current width of the image in (0.01)mm
Dim picw As Integer = CInt(Math.Round((Bmp.Width / xDpi) * HMM_PER_INCH))
' Calculate the current height of the image in (0.01)mm
Dim pich As Integer = CInt(Math.Round((Bmp.Height / yDpi) * HMM_PER_INCH))
' Calculate the target width of the image in twips
Dim picwgoal As Integer = CInt(Math.Round((Bmp.Width / xDpi) * TWIPS_PER_INCH))
' Calculate the target height of the image in twips
Dim pichgoal As Integer = CInt(Math.Round((Bmp.Height / yDpi) * TWIPS_PER_INCH))
' Append values to RTF string
_rtf.Append("{\pict\wbitmap0")
_rtf.Append("\picw")
_rtf.Append(Bmp.Width.ToString)
' _rtf.Append(picw.ToString)
_rtf.Append("\pich")
_rtf.Append(Bmp.Height.ToString)
' _rtf.Append(pich.ToString)
_rtf.Append("\wbmbitspixel24\wbmplanes1")
_rtf.Append("\wbmwidthbytes40")
_rtf.Append("\picwgoal")
_rtf.Append(picwgoal.ToString)
_rtf.Append("\pichgoal")
_rtf.Append(pichgoal.ToString)
_rtf.Append("\bin ")
_rtf.Append(str.ToLower & "}")
Return _rtf.ToString
End Function
Public Function FormatText(ByVal data As String, ByVal newColor As fColorEnum) As String
data = System.Net.WebUtility.HtmlDecode(data)
data = data.Replace("|", " ")
Dim reg As New Regex("\$(.[0-9]+)\$")
If reg.IsMatch(data) Then
Dim meep As String = Regex.Match(data, "\$(.[0-9]+)\$").Groups(1).ToString
Dim idx As Integer = Convert.ToInt32(meep)
Dim img As String = Fox2RTF(idx)
If img IsNot Nothing Then data = Regex.Replace(data, "\$(.[0-9]+)\$", img)
End If
Dim myColor As System.Drawing.Color = fColor(newColor)
Dim ColorString = "{\colortbl ;"
ColorString += "\red" & myColor.R & "\green" & myColor.G & "\blue" & myColor.B & ";}"
Dim FontSize As Integer = cMain.ApFont.Size
Dim FontFace As String = cMain.ApFont.Name
FontSize *= 2
Dim test As String = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033" & ColorString & "{\fonttbl{\f0\fcharset0 " & FontFace & ";}}\viewkind4\uc1\fs" & FontSize.ToString & data & "\par}"
Return "{\rtf1\ansi\ansicpg1252\deff0\deflang1033" & ColorString & "{\fonttbl{\f0\fcharset0 " & FontFace & ";}}\viewkind4\uc1\fs" & FontSize.ToString & data & "\cf0 \par}"
End Function
Private Function Fox2RTF(ByRef Img As Integer) As String
Dim shape As New FurcadiaShapes(Paths.GetDefaultPatchPath() & "system.fsh")
Dim anims As Bitmap() = Helper.ToBitmapArray(shape)
' pic.Image = anims(Img)
Return PictureToRTF.PictureToRTF(anims(Img))
End Function
Never found the Soultion I was hoping for with this.. But I did find a work around http://www.codeproject.com/Articles/30902/RichText-Builder-StringBuilder-for-RTF