How to Re-size image on File Upload - vb.net

I am trying to resize the image that is being uploaded using FileUpload control in ASP.net using VB.NET
I am using a file upload code:
If FileUpload1.HasFile Then
Dim theFileName As String = Path.Combine(Server.MapPath("~/Uploads"), newfile)
FileUpload1.SaveAs(theFileName)
End If
How do I resize the image to width:270px height:307px?

Well, I hope this help :
Dim bmp As New Bitmap(FileUpload1.PostedFile.InputStream)
Dim newImage = New Bitmap(newWidth, newHeight)
Using g = Graphics.FromImage(newImage)
g.DrawImage(bmp, 0, 0, newWidth, newHeight)
End Using

Related

Taking a screenshot of only portion of the browser using selenium in VBA | "Selenium VBA"

so this one is about taking a screenshot of only a portion of the browser using Selenium in VBA. I saw there is something like
takescreenshot.hieght
takescreenshot.width
but not sure how to use these sub attributes height and width or if they will work.
Plus I have multiple images to save using different filenames automatically. I tried this but it did not work.
dim bot as chromedriver, img as selenium.image
bot.get "http:// xxxxxxx.....com"
..
..
for a = 1 to 100
set img = bot.takescreenshot(500)
img.Saveas this workbook.path & " :\" & a & ".jpg"
any suggestions please. Thanks in advance
There is unfortunately no built in function to take partial screenshots. However, you might try the following function to get an image of a specific element--please forgive any vb related mistakes I may have made as I rarely use the language.
Public Function CaptureElementScreenShot(ByVal element As HTMLElement, ByVal uniqueName As String) As Image
Dim screenshot As Screenshot = (CType(Me.driver, ITakesScreenshot)).GetScreenshot()
screenshot.SaveAsFile(filename, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim img As Image = Bitmap.FromFile(uniqueName)
Dim rect As Rectangle = New Rectangle()
If element IsNot Nothing Then
Dim width As Integer = element.Size.Width
Dim height As Integer = element.Size.Height
Dim p As Point = element.Location
rect = New Rectangle(p.X, p.Y, width, height)
End If
Dim bmpImage As Bitmap = New Bitmap(img)
Dim croppedImage = bmpImage.Clone(rect, bmpImage.PixelFormat)
Return croppedImage
End Function

Create a new picture along the GraphicsPath

Is there some way to copy a GraphicsPath and the enclosed figure into a new picture?
I have the rectangle, the points of the GraphicsPath available. The path is definitely in the rectangle.
I already googled but the result is poor. So far, I can only copy a certain area (rectangle) into a new picture, see source code.
Using Extracted As Bitmap = New Bitmap(rect.Width, rect.Height, Imaging.PixelFormat.Format32bppArgb)
Using Grp As Graphics = Graphics.FromImage(Extracted)
Grp.DrawImage(Picture1, 0, 0, rect, GraphicsUnit.Pixel)
End Using
If System.IO.Directory.Exists("C:\Users\xy\Desktop") Then
Extracted.Save("C:\Users\xy\Desktop\1.png", Imaging.ImageFormat.Png)
End If
End Using
I have found something here:
This is the solution translated into VB.Net and with Option Strict On and Option Infer Off.
Using bmpSource As Bitmap = New Bitmap(Form1.Pfad_Bild)
Dim rectCutout As RectangleF = gp.GetBounds()
Using m As Matrix = New Matrix()
m.Translate(-rectCutout.Left, -rectCutout.Top)
gp.Transform(m)
End Using
Using bmpCutout As Bitmap = New Bitmap(CInt(rectCutout.Width), CInt(rectCutout.Height))
Using graphicsCutout As Graphics = Graphics.FromImage(bmpCutout)
graphicsCutout.Clip = New Region(gp)
graphicsCutout.DrawImage(bmpSource, CInt(-rectCutout.Left), CInt(-rectCutout.Top))
If System.IO.Directory.Exists("C:\Users\xy\Desktop") Then
bmpCutout.Save("C:\Users\xy\Desktop\1.png", Imaging.ImageFormat.Png)
End If
End Using
End Using
End Using

how can i convert a bitmap to mat VB.net

i am working in a project where i have to convert a bitmap image from a screenshot to a mat format directly in order to use it with engu opencv. it only works when i save the image and call it with New Mat() funtion.
Public Function ScreenShot() As Bitmap
Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width,
My.Computer.Screen.Bounds.Height)
Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(screenGrab)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
screenGrab.Save("C:\Users\Desktop\test.png")
imgOriginal = New Mat("C:\Users\Desktop\test.png")
screengrab is the bitmap but how can i use it directly as a mat format without saving it?.
i think this saving operation could damage the hard drive because it is saving and reading this every second.

WP8 - Display the photo i have taken in the control Image

Title explains everything.
How should i do ?
I coded this :
Dim size As Windows.Foundation.Size
Dim seq As CameraCaptureSequence
Dim imageStream As New MemoryStream
If PhotoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Back) Then
Dim avalaibleSizeList As IReadOnlyList(Of Windows.Foundation.Size) = PhotoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back)
size = avalaibleSizeList(0)
Me.captureDevice = Await PhotoCaptureDevice.OpenAsync(CameraSensorLocation.Back, size)
End If
seq = captureDevice.CreateCaptureSequence(1)
seq.Frames(0).CaptureStream = imageStream.AsOutputStream()
Await captureDevice.PrepareCaptureSequenceAsync(seq)
Await seq.StartCaptureAsync()
imageStream.Seek(0, SeekOrigin.Begin)
Dim library As New MediaLibrary
Dim picture As Picture = library.SavePictureToCameraRoll("PhotosIncidents", imageStream)
But what i am suppose to do now?
You can create a BitmapImage, set it's source to your imageStream. Then put an Image control in XAML to display BitmapImage in your application :
Dim bitmapImage As BitmapImage
bitmapImage.SetSource(imageStream)
MyImageControl.Source = bitmapImage
Note : I havent tested the code, but thats what I will try if I need to do the same.

byte array to image

i am trying to convert a byte array into an image
here is my code:
Public img As BitmapImage
Public bytes As Byte()
Sub convert()
Using ms As New MemoryStream(bytes, 0, bytes.Length)
ms.Write(bytes, 0, bytes.Length)
'Dim bitmg As New BitmapImage
'bitmg.SetSource(ms)
img.SetSource(ms)
End Using
End Sub
but when i am running the application i get this error : Null reference was unhandled
on the last line
img.SetSource(ms)
Any ideas ? thank you
You haven't shown any code which actually gives img a value. Do you have
img = New BitmapImage
anywhere?