(MVC) MVC (2015 год)

Загально графічні функції.

Я постійно працюю з графікою GDI+, наприклад вся моя робота з терміналами була в значної частині роботою з графікою, подивиться лише на кнопки (це була моя власна реалізація XAML) - мабуть треба знайти вільний час і хочь трохи описати цей міє проєкт.

А ось тут описана одна з моїх останніх графічних прог - Конструктор PDF-схем.

Також я постійно публікую якісь свої добре відпрацьовані графічні функціі, почав я це робити ще десять років тому - Наложение копирайта на рисунки.

А на цієй сторінці я додам ще трохи своїх добре відпрацьованих графічних функцій.



   1:  Imports Microsoft.VisualBasic
   2:   
   3:  Public Class ImageService
   4:   
   5:      'размеры
   6:      Public Function GetDimension(ByVal Buffer As Byte()) As Dimension
   7:          Dim Ret1 As Dimension
   8:          Using Buf As New IO.MemoryStream(Buffer)
   9:              Using Source As New Drawing.Bitmap(Buf, False)
  10:                  Ret1.Width = Source.Width
  11:                  Ret1.Heigth = Source.Height
  12:              End Using
  13:          End Using
  14:          Return Ret1
  15:      End Function
  16:   
  17:      ' Уменьшить рисунок до заданной ширины
  18:      Public Function StripSizeForWidth(ByVal Buffer As Byte(), ByVal NewWidth As Integer) As Byte()
  19:          Try
  20:              If Buffer.Length > 0 And NewWidth > 0 Then
  21:                  Dim Out As New IO.MemoryStream
  22:                  Dim Buf As New IO.MemoryStream(Buffer)
  23:                  Dim Source As New Drawing.Bitmap(Buf, False)
  24:                  Dim Reduce As Double = Source.Width / NewWidth
  25:                  Dim Target As New Drawing.Bitmap(Source, CInt(NewWidth), CInt(Source.Height / Reduce))
  26:                  Select Case Target.RawFormat.Guid
  27:                      Case Drawing.Imaging.ImageFormat.MemoryBmp.Guid : Target.Save(Out, System.Drawing.Imaging.ImageFormat.Jpeg)
  28:                      Case Else : Throw New Exception("Неизвестный формат графического файла")
  29:                  End Select
  30:                  Return Out.ToArray
  31:              Else
  32:                  Throw New Exception("StripSize.")
  33:              End If
  34:          Catch ex As Exception
  35:              Throw New Exception("StripSize: " & ex.Message)
  36:          End Try
  37:      End Function
  38:   
  39:      ' Уменьшить рисунок до заданной высоты
  40:      Public Function StripSizeForHeight(ByVal Buffer As Byte(), ByVal NewHeight As Integer) As Byte()
  41:          Try
  42:              If Buffer.Length > 0 And NewHeight > 0 Then
  43:                  Dim Out As New IO.MemoryStream
  44:                  Dim Buf As New IO.MemoryStream(Buffer)
  45:                  Dim Source As New Drawing.Bitmap(Buf, False)
  46:                  Dim Reduce As Double = Source.Height / NewHeight
  47:                  Dim Target As New Drawing.Bitmap(Source, CInt(Source.Width / Reduce), CInt(NewHeight))
  48:                  Select Case Target.RawFormat.Guid
  49:                      Case Drawing.Imaging.ImageFormat.MemoryBmp.Guid : Target.Save(Out, System.Drawing.Imaging.ImageFormat.Jpeg)
  50:                      Case Else : Throw New Exception("Неизвестный формат графического файла")
  51:                  End Select
  52:                  Return Out.ToArray
  53:              Else
  54:                  Throw New Exception("StripSize.")
  55:              End If
  56:          Catch ex As Exception
  57:              Throw New Exception("StripSize: " & ex.Message)
  58:          End Try
  59:   
  60:      End Function
  61:   
  62:      ' Растянуть рисунок
  63:      Public Function ReSize(ByVal Buffer As Byte(), ByVal NewHeight As Integer, ByVal NewWidth As Integer) As Byte()
  64:          Try
  65:              Dim Out As New IO.MemoryStream
  66:              Dim Buf As New IO.MemoryStream(Buffer)
  67:              Dim Source As New Drawing.Bitmap(Buf, False)
  68:              Dim Target As New Drawing.Bitmap(Source, CInt(NewWidth), CInt(NewHeight))
  69:              Select Case Target.RawFormat.Guid
  70:                  Case Drawing.Imaging.ImageFormat.MemoryBmp.Guid : Target.Save(Out, System.Drawing.Imaging.ImageFormat.Jpeg)
  71:                  Case Else : Throw New Exception("Неизвестный формат графического файла")
  72:              End Select
  73:              Return Out.ToArray
  74:          Catch ex As Exception
  75:              Throw New Exception("StripSize: " & ex.Message)
  76:          End Try
  77:      End Function
  78:   
  79:   
  80:      'кеш с ограничением по большой стороне рисунка
  81:      Public Function StripSize(Buf As Byte(), Dimension As ImageService.Dimension, CacheImageSise As Integer) As Byte()
  82:          If Dimension.Width > Dimension.Heigth Then
  83:              StripSize = ImageService.StripSizeForWidth(Buf, CacheImageSise)
  84:          Else
  85:              StripSize = ImageService.StripSizeForHeight(Buf, CacheImageSise)
  86:          End If
  87:          Return StripSize
  88:      End Function
  89:   
  90:      'усечение
  91:      Public Function Crop(ByVal Buffer As Byte(), X1 As Integer, Y1 As Integer, Width As Integer, Heigth As Integer) As Byte()
  92:          Try
  93:              Dim Out As New IO.MemoryStream
  94:              Dim Buf As New IO.MemoryStream(Buffer)
  95:              Dim Source As New Drawing.Bitmap(Buf, False)
  96:              Dim Target As New Drawing.Bitmap(Width, Heigth)
  97:              Dim Area As New Drawing.Rectangle(X1, Y1, Width, Heigth)
  98:              Dim G As Drawing.Graphics = Drawing.Graphics.FromImage(Target)
  99:              G.DrawImage(Source, 0, 0, Area, Drawing.GraphicsUnit.Pixel)
 100:              Select Case Target.RawFormat.Guid
 101:                  Case Drawing.Imaging.ImageFormat.MemoryBmp.Guid : Target.Save(Out, System.Drawing.Imaging.ImageFormat.Jpeg)
 102:                  Case Else : Throw New Exception("Неизвестный формат графического файла")
 103:              End Select
 104:              Return Out.ToArray
 105:          Catch ex As Exception
 106:              Throw New Exception("StripSize: " & ex.Message)
 107:          End Try
 108:      End Function
 109:   
 110:      'поворот
 111:      Public Function Rotate(ByVal Buffer As Byte(), Rotate1 As Integer) As RotateOut
 112:          Dim OutBytes(10000000) As Byte
 113:          Dim Memory As New System.IO.MemoryStream(OutBytes)
 114:          Dim ImgFormat As System.Drawing.Imaging.ImageFormat
 115:          Dim Ret1 As Dimension
 116:          Using Buf As New IO.MemoryStream(Buffer)
 117:              Using Source As New System.Drawing.Bitmap(Buf, False)
 118:                  ImgFormat = Source.RawFormat
 119:                  If Rotate1 = 0 Then
 120:                      Source.RotateFlip(Drawing.RotateFlipType.Rotate90FlipNone)
 121:                  Else
 122:                      Source.RotateFlip(Drawing.RotateFlipType.Rotate270FlipNone)
 123:                  End If
 124:                  Ret1.Width = Source.Width
 125:                  Ret1.Heigth = Source.Height
 126:                  Source.Save(Memory, Drawing.Imaging.ImageFormat.Jpeg)
 127:                  ReDim Preserve OutBytes(Memory.Position)
 128:              End Using
 129:          End Using
 130:          Return New RotateOut With {.Dimension = Ret1, .ImageFormat = ImgFormat, .ImageBytes = OutBytes}
 131:      End Function
 132:  End Class
 133:   
 134:  Public Structure Dimension
 135:      Dim Width As Integer
 136:      Dim Heigth As Integer
 137:  End Structure
 138:   
 139:  Public Class RotateOut
 140:      Property Dimension As ImageService.Dimension
 141:      Property ImageFormat As System.Drawing.Imaging.ImageFormat
 142:      Property ImageBytes As Byte()
 143:      Property Length As Integer
 144:  End Class


Comments ( )
Link to this page: //www.vb-net.com/GDI-Plus/index.htm
< THANKS ME>