2

我正在 VB.NET 中开发一个小型应用程序,我需要将手机中的图像加载到图片框中。不过,我无法将图片添加到图片框中。它抛出 OutOfMemoryException。我想知道是否有任何方法可以通过将图片更改为缩略图来减小图片的大小。

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
4

2 回答 2

1

这是我针对该问题的 OpenNetCF C# 片段:

    ...
    //图像工厂
    使用 OpenNETCF.Drawing;
    使用 OpenNETCF.Drawing.Imaging;
    ...
            OpenNETCF.Drawing.Imaging.StreamOnFile m_stream;
            尺寸 m_size;
            ///
            /// 这也将处理大位图并在图片框上显示缩略图版本
            /// 见 http://blog.opennetcf.com/ctacke/2010/10/13/LoadingPartsOfLargeImagesInTheCompactFramework.aspx
            ///
            /// 要加载的文件名
            私人无效showImage(字符串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);
                显示快照(真);//显示静止图像
                m_stream.Dispose();
                流。关闭();
            }

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

于 2012-09-05T04:00:19.340 回答
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
于 2012-09-11T13:11:15.160 回答