这是上面的VB代码:
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports OpenNETCF.Drawing
Imports OpenNETCF.Drawing.Imaging
Imports System.Runtime.InteropServices
Imports System.Resources
Imports System.Reflection
Public Class ImageHelper
Private Shared m_factory As ImagingFactory
Private Shared Function GetFactory() As ImagingFactory
If (m_factory Is Nothing) Then
m_factory = New ImagingFactory
End If
Return m_factory
End Function
Public Shared Function CreateClip(ByVal sof As StreamOnFile, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) As Bitmap
Dim original As IBitmapImage = Nothing
Dim image As IImage = Nothing
Dim info As ImageInfo
GetFactory.CreateImageFromStream(sof, image)
Try
image.GetImageInfo(info)
GetFactory.CreateBitmapFromImage(image, info.Width, info.Height, info.PixelFormat, InterpolationHint.InterpolationHintDefault, original)
Try
Dim ops As IBasicBitmapOps = CType(original, IBasicBitmapOps)
Dim clip As IBitmapImage = Nothing
Try
Dim rect As RECT = New RECT(x, y, (x + width), (y + height))
ops.Clone(rect, clip, True)
Return ImageUtils.IBitmapImageToBitmap(clip)
Finally
Marshal.ReleaseComObject(clip)
End Try
Finally
Marshal.ReleaseComObject(original)
End Try
Finally
Marshal.ReleaseComObject(image)
End Try
End Function
Public Shared Function getScaledBitmap(ByVal sof As StreamOnFile, ByVal scalePercent As Integer) As Bitmap
Dim thumbnail As IBitmapImage = Nothing
Dim image As IImage = Nothing
Dim info As ImageInfo
Dim fScale As Decimal = (scalePercent / 100)
' m()
' do not remove the m specifier!
GetFactory.CreateImageFromStream(sof, image)
Try
image.GetImageInfo(info)
Dim newWidth As UInteger = CType((info.Width * fScale), UInteger)
Dim newHeight As UInteger = CType((info.Height * fScale), UInteger)
GetFactory.CreateBitmapFromImage(image, newWidth, newHeight, info.PixelFormat, InterpolationHint.InterpolationHintDefault, thumbnail)
Try
Return ImageUtils.IBitmapImageToBitmap(thumbnail)
Catch ex As Exception
Dim stream As System.IO.Stream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("CameraCx7x.oom.png")
Return New Bitmap(stream)
' (400, 300);
Finally
Marshal.ReleaseComObject(thumbnail)
End Try
Finally
Marshal.ReleaseComObject(image)
End Try
End Function
Public Shared Function CreateThumbnail(ByVal sof As StreamOnFile, ByVal width As Integer, ByVal height As Integer) As Bitmap
Dim thumbnail As IBitmapImage = Nothing
Dim image As IImage = Nothing
Dim info As ImageInfo
GetFactory.CreateImageFromStream(sof, image)
Try
image.GetImageInfo(info)
GetFactory.CreateBitmapFromImage(image, CType(width, UInteger), CType(height, UInteger), info.PixelFormat, InterpolationHint.InterpolationHintDefault, thumbnail)
Try
Return ImageUtils.IBitmapImageToBitmap(thumbnail)
Finally
Marshal.ReleaseComObject(thumbnail)
End Try
Finally
Marshal.ReleaseComObject(image)
End Try
End Function
Public Shared Function saveScaledBitmap(ByVal sof As StreamOnFile, ByVal width As Integer, ByVal height As Integer, ByVal sNewFile As String) As Boolean
Dim bRet As Boolean = True
Try
Dim bmp As Bitmap = CreateThumbnail(sof, width, height)
bmp.Save(sNewFile, System.Drawing.Imaging.ImageFormat.Jpeg)
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(("Exception in saveScaledBitmap(): " + ex.Message))
bRet = False
End Try
Return bRet
End Function
Public Shared Function GetRawImageSize(ByVal sof As StreamOnFile) As Size
Dim image As IImage = Nothing
Dim info As ImageInfo
GetFactory.CreateImageFromStream(sof, image)
Try
image.GetImageInfo(info)
Return New Size(CType(info.Width, Integer), CType(info.Height, Integer))
Finally
Marshal.ReleaseComObject(image)
End Try
End Function
End Class
这就是如何从带有标签、按钮和图片框的表单中调用它
Imports OpenNETCF.Drawing
Imports OpenNETCF.Drawing.Imaging
Imports System.IO
Public Class Form1
Dim _Bitmap As Bitmap
Private Sub showImage(ByVal filePath As String)
'do not load the full image!
If File.Exists(filePath) Then
label1.Text = filePath
If (Not (_Bitmap) Is Nothing) Then
_Bitmap.Dispose()
End If
' a 1944x2593 image is about 15MByte !
'create a scaled down image file
Dim _filestream As FileStream = File.Open(filePath, FileMode.Open)
Dim _stream As StreamOnFile = New StreamOnFile(_filestream)
Dim fi As System.IO.FileInfo = New FileInfo(filePath)
Dim sizeBmp As Size = ImageHelper.GetRawImageSize(_stream)
'allow 640 hight
Dim ratio As Integer = (sizeBmp.Height / 640)
Dim scale As Integer = (ratio * 10)
If (ratio > 1) Then
_Bitmap = ImageHelper.getScaledBitmap(_stream, scale)
End If
Me.pictureBox1.Image = CType(_Bitmap, Image)
_filestream.Close()
End If
End Sub
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Dim ofd As New OpenFileDialog
ofd.InitialDirectory = "\My Documents"
ofd.Filter = "Jpg image|*.jpg|Bmp image|*.bmp|Gif image|*.gif|All files|*.*"
ofd.FilterIndex = 0
Dim sFile As String
If (ofd.ShowDialog = Windows.Forms.DialogResult.OK) Then
sFile = ofd.FileName
ofd.Dispose()
showImage(sFile)
End If
End Sub
End Class