Click here to Skip to main content
15,937,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! all,
I have created a image capturing code in VB.Net, for this i used the interop.WIA.dll,
the problem i am facing is when i capture the image, it's size is big and i want to resize it's resolution and size to small.
Following is the code i'm using to capture the image and i can't resize the image.
Private Sub btnGrab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrab.Click
        Dim item As WIA.Item
            'executes the device's TakePicture command
            item = SelectedDevice.ExecuteCommand(WIA.CommandID.wiaCommandTakePicture)
        Catch ex As System.Exception
            MessageBox.Show("Problem Taking Picture. Please make sure that the camera is plugged in and is not in use by another application. " & vbCrLf & "Extra Info:" & ex.Message, "Problem Grabbing Picture", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
            Exit Sub
        End Try
        Dim jpegGuid As String
        'retrieves jpegKey from registry, used in saving JPEG
        Dim jpegKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("CLSID\{D2923B86-15F1-46FF-A19A-DE825F919576}\SupportedExtension\.jpg")
        jpegGuid = CType(jpegKey.GetValue("FormatGUID"), String)
        'loops through available formats for the captured item, looking for the JPG format
        For Each format As String In item.Formats
            If (format = jpegGuid) Then
                'transfers image to an imagefile object
                Dim imagefile As WIA.ImageFile = CType(item.Transfer(format), WIA.ImageFile)
                Dim Counter As Integer 'counter in loop appended to filename
                Dim LoopAgain As Boolean = True
                Dim height As Int32 = CInt(Val(10))
                Dim width As Int32 = CInt(Val(10))

                'searches directory, gets next available picture name
                Do Until LoopAgain = False
                    Dim Filename As String = SavePath & "\" & txtPrefix.Text & Counter.ToString & ".jpg"
                    'if file doesnt exist, save the file
                    If Not System.IO.File.Exists(Filename) Then
                        SavedFilePath = Filename
                        imagefile.SaveFile(Filename) 'saves file to disk
                        lblCapName.Text = FormatPath(Filename)
                        lblImageName.Text = txtPrefix.Text & Counter.ToString & ".jpg"
                        picCap.Image = Image.FromFile(Filename) 'loads captured file to picturebox
                        LoopAgain = False
                    End If
                    Counter = Counter + 1
            End If
        If grpSaved.Enabled = False Then
            grpSaved.Enabled = True
        End If
    End Sub
Updated 28-Apr-11 23:36pm
Nischal Bhatt 29-Apr-11 5:43am    
You can have a look at this link for my question.

Hope Image Cropping with Image Resizing Using VB.NET[^] article from CP might help you.
Share this answer
Here is a handy little routine for resizing images

private static Image resizeImage(Image imgToResize, Size sizeOfNewImage)
            Bitmap bmp = new Bitmap(sizeOfNewImage.Width, sizeOfNewImage.Height);
            Graphics gra = Graphics.FromImage((Image)bmp);
            gra.InterpolationMode = InterpolationMode.HighQualityBicubic;//Can try various other quality enumerations
            gra.DrawImage(imgToResize, 0, 0, sizeOfNewImage.Width, sizeOfNewImage.Height);
            return (Image)bmp;

Hope this helps
Share this answer
Nischal Bhatt 29-Apr-11 6:28am    
I tried this but it's not working...don't know why..
Wayne Gaylard 29-Apr-11 7:37am    
Did it throw an exception or what was the problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900