VB.Net程序设计:Image图片处理 GDI+绘图技术

来源:互联网 发布:unity3d vuforia 教程 编辑:程序博客网 时间:2024/05/17 08:59
用到的知识点:
获取图片的缩略图
鼠标拽区,截取图片的指定区域
在Panel控件上绘图,绘制鼠标拽出来的矩形。双重缓存的运用。Invalidate、Paint重绘控件。
Bitmap,Image,Rectangle运用。 图片缩放。

 

程序代码 下载地址:http://download.csdn.net/source/633179

 

程序界面:

--

 

--

下面是主要代码:

  1. '绘制图片
  2.     Private Sub panel1_Paint(ByVal sender As ObjectByVal e As PaintEventArgs) Handles Panel1.Paint
  3.         MyBase.OnPaint(e)
  4.         Me.InvokePaintBackground(Me.Panel1, e)
  5.         If x0 < x1 Then
  6.             RcDraw.X = x0
  7.             RcDraw.Width = x1 - x0
  8.         Else
  9.             RcDraw.X = x1
  10.             RcDraw.Width = x0 - x1
  11.         End If
  12.         If y0 < y1 Then
  13.             RcDraw.Y = y0
  14.             RcDraw.Height = y1 - y0
  15.         Else
  16.             RcDraw.Y = y1
  17.             RcDraw.Height = y0 - y1
  18.         End If
  19.         If imageStatus Then
  20.             WorkImg = New Bitmap(StartImg)
  21.             Using gb As Graphics = Graphics.FromImage(WorkImg)
  22.                 RcDraw.Offset(-ImgBounds.X, -ImgBounds.Y)
  23.                 If RcDraw.X < 0 Then RcDraw.X = 0
  24.                 If RcDraw.Y < 0 Then RcDraw.Y = 0
  25.                 rcSel = RcDraw
  26.                 If RcDraw.X + RcDraw.Width >= ImgBounds.Width Then
  27.                     RcDraw.Width = ImgBounds.Width - RcDraw.X - 1
  28.                     rcSel.Width += 1
  29.                 End If
  30.                 If RcDraw.Y + RcDraw.Height >= ImgBounds.Height Then
  31.                     RcDraw.Height = ImgBounds.Height - RcDraw.Y - 1
  32.                     rcSel.Width += 1
  33.                 End If
  34.                 gb.DrawRectangle(pen, RcDraw)
  35.             End Using
  36.             e.Graphics.DrawImage(WorkImg, ImgBounds)
  37.             'Using g As Graphics = e.Graphics
  38.             '    g.Clear(Color.White)
  39.             '    '重新画背景图
  40.             '    g.DrawImage(WorkImg, rcDest)
  41.             '    '画裁剪框
  42.             '    g.DrawRectangle(pen, RcDraw)
  43.             '    'Me.Panel1.BackgroundImage = WorkImg
  44.             'End Using
  45.         End If
  46.     End Sub
  47.     '选择图片
  48.     Private Sub BtOpen_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles BtOpen.Click
  49.         Dim fd As New OpenFileDialog()
  50.         'fd.InitialDirectory = "D:/"
  51.         fd.Filter = "图像文件Image File|*.jpg;*.gif;*.png"
  52.         If fd.ShowDialog() = Windows.Forms.DialogResult.OK Then
  53.             CreateImage(fd.FileName)
  54.             DrawImg()
  55.         End If
  56.     End Sub
  57.     '打开图片
  58.     Private Sub CreateImage(ByVal path As String)
  59.         If path.Equals(String.Empty) Then
  60.             SrcImg = Nothing
  61.             ShowMsg("空的图片路径。", MsgType.Err)
  62.         Else
  63.             SrcImg = New Bitmap(path)
  64.             imageStatus = True
  65.             ShowMsg("图片路径。" & path)
  66.         End If
  67.         CreateWorkingImage(SrcImg)
  68.     End Sub
  69.     '创建工作图片
  70.     Private Sub CreateWorkingImage(ByVal srcImage As Image)
  71.         If Not (StartImg Is NothingThen
  72.             StartImg.Dispose()
  73.             StartImg = Nothing
  74.         End If
  75.         If Not (WorkImg Is NothingThen
  76.             WorkImg.Dispose()
  77.             WorkImg = Nothing
  78.         End If
  79.         Dim maxSize As Integer = CInt(Math.Max(Me.Panel1.Width, Me.Panel1.Height))
  80.         If srcImage.Width <= maxSize And srcImage.Height <= maxSize Then
  81.             _scale = 1.0F
  82.             StartImg = New Bitmap(srcImage)
  83.         Else
  84.             _scale = CSng(Math.Max(srcImage.Width, srcImage.Height)) / CSng(maxSize)
  85.             Dim width As Integer = CInt(CSng(srcImage.Width) / _scale)
  86.             Dim height As Integer = CInt(CSng(srcImage.Height) / _scale)
  87.             StartImg = New Bitmap(width, height)
  88.             Dim g As Graphics = Graphics.FromImage(StartImg)
  89.             Try
  90.                 g.InterpolationMode = InterpolationMode.Bilinear
  91.                 Dim rcDest As New Rectangle(0, 0, width, height)
  92.                 Dim dsDest As New Rectangle(0, 0, srcImage.Width, srcImage.Height)
  93.                 g.DrawImage(srcImage, rcDest, dsDest, GraphicsUnit.Pixel)
  94.             Finally
  95.                 g.Dispose()
  96.             End Try
  97.         End If
  98.         WorkImg = New Bitmap(StartImg)
  99.     End Sub
  100.     '绘图工作图片
  101.     Private Sub DrawImg()
  102.         Dim KrctWidth As Integer = Math.Max(Krct.Width, 0)
  103.         Dim KrctHeight As Integer = Math.Max(Krct.Height, 0)
  104.         Dim ratio As Single = Math.Max(CSng(WorkImg.Width / KrctWidth), CSng(WorkImg.Height / KrctHeight))
  105.         Dim width As Integer = CInt(CSng(WorkImg.Width) / ratio)
  106.         Dim height As Integer = CInt(CSng(WorkImg.Height) / ratio)
  107.         Dim px, py As Integer
  108.         px = (Math.Max(WorkImg.Width, KrctWidth) - Math.Min(WorkImg.Width, KrctWidth)) / 2
  109.         py = (Math.Max(WorkImg.Height, KrctHeight) - Math.Min(WorkImg.Height, KrctHeight)) / 2
  110.         ImgBounds = New Rectangle(px, py, WorkImg.Width, WorkImg.Height)
  111.         Using g As Graphics = Panel1.CreateGraphics()
  112.             g.DrawImage(WorkImg, ImgBounds)
  113.         End Using
  114.     End Sub
  1.    '放大图片
  2.     Private Sub BtAddSize_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles BtAddSize.Click
  3.         Dim g As Graphics = Panel2.CreateGraphics()
  4.         If SmallBmp IsNot Nothing Then
  5.             resizeLevel = resizeLevel + 1
  6.             Dim fac As Single = CSng((1 + (resizeLevel * 0.25)))
  7.             Dim w As Integer = CInt((SmallBmp.Width * fac))
  8.             Dim h As Integer = CInt((SmallBmp.Height * fac))
  9.             Dim rd As New Rectangle(0, 0, w, h)
  10.             Dim tempBmp As New Bitmap(w, h)
  11.             Dim gi As Graphics = Graphics.FromImage(tempBmp)
  12.             gi.DrawImage(SmallBmp, rd)
  13.             g.DrawImage(tempBmp, rd)
  14.             gi.Dispose()
  15.         End If
  16.         g.Dispose()
  17.     End Sub
  18.     '缩小图片
  19.     Private Sub BtMinSize_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles BtMinSize.Click
  20.         Dim g As Graphics = Panel2.CreateGraphics()
  21.         If SmallBmp IsNot Nothing Then
  22.             resizeLevel = IIf((resizeLevel > -3), resizeLevel - 1, resizeLevel)
  23.             Dim fac As Single = CSng((1 + (resizeLevel * 0.25)))
  24.             Dim w As Integer = CInt((SmallBmp.Width * fac))
  25.             Dim h As Integer = CInt((SmallBmp.Height * fac))
  26.             Dim rd As New Rectangle(0, 0, w, h)
  27.             Dim tempBmp As New Bitmap(w, h)
  28.             Dim gi As Graphics = Graphics.FromImage(tempBmp)
  29.             g.FillRectangle(Brushes.White, Panel2.ClientRectangle)
  30.             gi.DrawImage(SmallBmp, rd)
  31.             g.DrawImage(tempBmp, rd)
  32.             gi.Dispose()
  33.         End If
  34.         g.Dispose()
  35.     End Sub
原创粉丝点击