100 likes | 163 Views
Create Program To Capture picture. ຮູບແບບໂປຣແກມ. ສ້າງ form. ຂຽນ code.
E N D
ຂຽນcode Imports System.DrawingImports System.Drawing.ImagingPublic Class frmMain Inherits System.Windows.Forms.Form Dim cropBitmap As Bitmap Dim cropX As Integer Dim cropY As Integer Dim cropWidth As Integer Dim cropHeight As Integer Public cropPen As Pen Public cropPenSize As Integer = 2 Public cropDashStyle As Drawing2D.DashStyle = Drawing2D.DashStyle.Solid Public cropPenColor As Color = Color.Aquamarine
Public c As CursorsPrivate Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click TryDim openDLG As New OpenFileDialog openDLG.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp;*.gif;*.jpg" If openDLG.ShowDialog = DialogResult.OK Then p.Image = Image.FromFile(openDLG.FileName, True) End If Catch exc As Exception MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End TryEnd Sub
Private Sub btnCrop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrop.Click Try If cropWidth < 1 Then MessageBox.Show("You need to first select what portion of the image to crop.", " No cropping Cordinates!", MessageBoxButtons.OK, MessageBoxIcon.Error) Exit Sub End If Dim bit As Bitmap = New Bitmap(p.Image, p.Width, p.Height) cropBitmap = New Bitmap(cropWidth, cropHeight) Dim g As Graphics = Graphics.FromImage(cropBitmap) g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel) pbCrop.Image = cropBitmap Catch exc As Exception MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End TryEnd Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click Try If pbCrop.Image Is Nothing Then MessageBox.Show("You have not edited the original image. There is no new image to save.Error MessageBoxButtons.OK, MessageBoxIcon.Information) Exit Sub End If Dim saveDLG As SaveFileDialog = New SaveFileDialog saveDLG.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp;*.gif;*.jpg" If saveDLG.ShowDialog = DialogResult.OK Then If saveDLG.FileName.EndsWith("bmp") Then pbCrop.Image.Save(saveDLG.FileName, ImageFormat.Bmp) ElseIf saveDLG.FileName.EndsWith("gif") Then pbCrop.Image.Save(saveDLG.FileName, ImageFormat.Gif) ElsepbCrop.Image.Save(saveDLG.FileName, ImageFormat.Jpeg) End If saveDLG.Dispose() End If Catch exc As Exception MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub
Private Sub p_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles p.MouseUp TryCursor = c.Default Catch exc As Exception MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub
Private Sub p_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles p.MouseMove Try If p.Image Is Nothing Then Exit Sub If e.Button = MouseButtons.Left Then p.Refresh() cropWidth = e.X - cropX cropHeight = e.Y - cropYp.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight) End IfGC.Collect() Catch exc As ExceptionIf Err.Number = 5 Then Exit Sub MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End TryEnd Sub