2012-09-04 47 views
2

我正在VB.NET中開發一個小應用程序,我需要從手機將圖片加載到圖片框中。不過,我無法將圖片添加到圖片框中。它拋出一個OutOfMemoryException異常。我想知道是否有任何方法通過將其更改爲縮略圖來縮小圖片的大小。如何在Windows Mobile應用程序中獲取圖片的縮略圖

Dim srcmap As New Bitmap(OpenFileDialog1.FileName) 
Dim destbit As New Bitmap(220, 220) 
Dim srcRec As New Rectangle(0, 0, srcmap.Width, srcmap.Height) 
Dim destRec As New Rectangle(0, 0, 220, 220) 
Dim g As Graphics 
g = Graphics.FromImage(destbit) 
g.DrawImage(srcmap, destRec,srcRec, GraphicsUnit.Pixel) 
picturebox.Image = destbit 
+0

某些圖像需要加載到應用程序的進程內存中。 OpenNetCF提供了一些使用流創建圖像縮略圖的功能。我只能在C#中提供一個示例: – josef

回答

1

這裏是我的OpenNetCF C#代碼段爲這個問題:

 ... 
    //imagefactory 
    using OpenNETCF.Drawing; 
    using OpenNETCF.Drawing.Imaging; 
    ... 
      OpenNETCF.Drawing.Imaging.StreamOnFile m_stream; 
      Size m_size; 
      /// 
      /// this will handle also large bitmaps and show a thumbnailed version on a picturebox 
      /// see http://blog.opennetcf.com/ctacke/2010/10/13/LoadingPartsOfLargeImagesInTheCompactFramework.aspx 
      /// 
      /// the name of the file to load 
      private void showImage(string sFileName) 
      { 
       var stream = File.Open(sFileName, FileMode.Open); 
       m_stream = new StreamOnFile(stream); 
       m_size = ImageHelper.GetRawImageSize(m_stream); 
       System.Diagnostics.Debug.WriteLine("showImage loading " + sFileName + ", width/height = " + m_size.Width.ToString() + "/"+ m_size.Height.ToString()); 
       //CameraPreview.Image = ImageHelper.CreateThumbnail(m_stream, CameraPreview.Width, CameraPreview.Height); 
       CameraSnapshot.Image = ImageHelper.CreateThumbnail(m_stream, CameraPreview.Width, CameraPreview.Height); 
       showSnapshot(true); //show still image 
       m_stream.Dispose(); 
       stream.Close(); 
      } 

與imagehelper.cs:HTTP://code.google.com/p/intermeccontrols/source/browse/DPAG7/Hasci .TestApp.And_Controls/IntermecControls/Hasci.TestApp.IntermecCamera3/ImageHelper.cs

+0

請問您是否願意在vb.net中幫助我,請 – Rizz

+0

以下是上述的VB代碼: – josef

0

下面是上述的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 
+0

它表示imagehelper未聲明。如何爲此添加參考,或者我可以知道此錯誤的解決方案嗎? – Rizz

+0

這簡直就是缺少Imports或者這樣的語句。我會盡快發佈完整的項目。 – josef

相關問題