VB.Net Tutorial/2D Graphics/Jpeg — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 16:40, 26 мая 2010

Create Jpeg File

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
public class MainClass
   public Shared Sub Main
        Dim bm As New Bitmap(256, 256)
        Dim gr As Graphics = Graphics.FromImage(bm)
        gr.Clear(Color.White)
        gr.DrawEllipse(Pens.Red, 0, 0, bm.Width - 1, bm.Height - 1)
        gr.DrawLine(Pens.Green, 0, 0, bm.Width - 1, bm.Height - 1)
        gr.DrawLine(Pens.Blue, bm.Width - 1, 0, 0, bm.Height - 1)
        " Save the result as a JPEG file.
        
        bm.Save("test.jpg", ImageFormat.Jpeg)
   End Sub
End class

Get Jpeg Information

" Quote from
"Visual Basic 2005 Cookbook Solutions for VB 2005 Programmers
"by Tim Patrick (Author), John Craig (Author)
"# Publisher: O"Reilly Media, Inc. (September 21, 2006)
"# Language: English
"# ISBN-10: 0596101775
"# ISBN-13: 978-0596101770

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Public Class GetJpgInformation
    Public Shared Sub Main
        Console.WriteLine(ProcessJPEG.GetJpgInformation("yourfile.jpg"))
    End Sub
End Class

Public Class ProcessJPEG
    Public Shared Function GetJpgInformation(ByVal whichFile As String) As String
        Dim bytesPropertyID As Byte()
        Dim stringPropertyID As String
        Dim loadedImage As System.Drawing.Bitmap
        Dim propertyIDs() As Integer
        Dim result As New System.Text.StringBuilder
        Dim counter As Integer
        Dim scanProperty As Integer
        loadedImage = New System.Drawing.Bitmap(whichFile)
        propertyIDs = loadedImage.PropertyIdList
        For Each scanProperty In propertyIDs
            bytesPropertyID = loadedImage.GetPropertyItem(scanProperty).Value
            stringPropertyID = System.Text.Encoding.ASCII.GetString(bytesPropertyID)
            For counter = 0 To 255
                If counter < 32 Or counter > 127 Then
                    If (stringPropertyID.IndexOf(Chr(counter)) _
                          <> -1) Then
                        stringPropertyID = Replace(stringPropertyID, _
                           Chr(counter), "")
                    End If
                End If
            Next counter
            " ----- Display the property if it"s reasonable.
            If (stringPropertyID.Length > 0) And _
                  (stringPropertyID.Length < 70) Then
                result.Append(scanProperty.ToString)
                result.Append(":   ")
                result.AppendLine(stringPropertyID)
            End If
        Next scanProperty
        " ----- Display the results.
        Return result.ToString
    End Function
    Public Shared Function GetString( _
      ByVal sourceBytes As Byte()) As String
        " ----- Convert a byte array to a string, taking into
        "       account the terminating null character.
        Dim result As String
        result = System.Text.Encoding.ASCII.GetString(sourceBytes)
        If (result.EndsWith(vbNullChar) = True) Then _
           result = result.Substring(0, result.Length - 1)
        Return result
    End Function
End Class

JPEG Compression

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Data
Imports System.Drawing.Imaging
public class MainClass
   public Shared Sub Main
        Dim curBitmap As Bitmap
        Dim imgCodecInfo As ImageCodecInfo = Nothing
        Dim encoder As Encoder
        Dim encoderParam As EncoderParameter
        Dim encoderParams As New EncoderParameters(1)
        curBitmap = New Bitmap("yourfile.jpg")
        Dim j As Integer
        Dim mimeType As String = "image/jpeg"
        Dim encoders() As ImageCodecInfo
        encoders = ImageCodecInfo.GetImageEncoders()
        j = 0
        While j < encoders.Length
            If encoders(j).MimeType = mimeType Then
                imgCodecInfo = encoders(j)
            End If
        End While "
        encoder = encoder.rupression
        encoderParam = New EncoderParameter(encoder, 1, CInt(EncoderParameterValueType.ValueTypeLong), 0)
        encoderParams.Param(0) = encoderParam "
        curBitmap.Save("Shape0.jpg", imgCodecInfo, encoderParams)
   End Sub
End class