简体   繁体   English

如何在Compact Framework上将PictureBox.Image设置为* .jpg-File?

[英]How to set PictureBox.Image to *.jpg-File on Compact Framework?

I am trying to develop a photo slideshow on a panel pc with Windows CE. 我正在尝试使用Windows CE在平板电脑上开发照片幻灯片。 Each of the images I want to show in a PictureBox on my formular is of *.jpg-Type. 我想在公式编辑器的图片框中显示的每个图像都是* .jpg类型的。 Files are eg ~1MB big with a resolution of 2304x1728. 文件的大小约为1MB,分辨率为2304x1728。 When I use following code, I get an OutOfMemory Exception. 当我使用以下代码时,出现OutOfMemory异常。

Bitmap bitmap = new Bitmap(file_name)
PictureBox.Image = bitmap

After researching I found out that the *.jpg-Files might be "too big" to fit into Bitmap. 经过研究,我发现* .jpg文件可能太大而无法放入Bitmap中。 With Compact Framework on VS2008 it is not possible for me to use something like 使用VS2008上的Compact Framework,我无法使用类似

Image image1 = Image.From(file_name)

How can I get an image from jpg-Files to my PictureBox ? 如何从jpg文件到PictureBox中获取图像?

EDIT[1]: 编辑[1]:

Thanks for your comments. 感谢您的意见。 I figured out that my pictures can not be loaded correctly because my device does not have enough memory to temporary load "huge" images. 我发现我的图片无法正确加载,因为我的设备没有足够的内存来临时加载“大”图像。 I solved the issue by writing some code that resizes my images before I copy them to my device. 我通过编写一些可调整图像大小的代码来解决该问题,然后再将其复制到设备中。

        Image bitmapNew = null;
        using (Bitmap bitmap = (Bitmap)Image.FromFile(filename))
        {
            double proportion = (double) bitmap.Width / bitmap.Height;
            proportion = Math.Round(proportion, 2);
            if (proportion > 1)
            {
               iWidth = iWidthMax;
               iHeight = (int)(iWidthMax / proportion);
            }
            else
            {
               iHeight = iHeightMax;
               iWidth = (int) (iHeightMax * proportion);
            }
            bitmapNew = new Bitmap(bitmap, new Size(iWidth, iHeight));

       }

The device´s resolution defines the parameter iWidthMax & iHeightMax 设备的分辨率定义了参数iWidthMaxiHeightMax

The file size of a JPG or any other compressed image file type does not say how many pixels are stored (5, 10 or more megapixel). JPG的文件大小或任何其他压缩图像文件类型都没有说明存储了多少像素(5、10或更大的像素)。 To show an image on screen, all pixels have to be loaded into memory. 为了在屏幕上显示图像,必须将所有像素加载到内存中。 For larger images this is impossible, especially on Windows Mobile with it's 32MB process slot limitation. 对于较大的图像,这是不可能的,尤其是在Windows Mobile具有32MB进程插槽限制的情况下。 And even if the whole image could be loaded it would not make sense, as the screen does not have that number of pixels and would just show only every 10th or 20th pixel. 而且即使可以加载整个图像,这也没有意义,因为屏幕没有像素数量,仅每10或20像素显示一次。

The best approach is the load only what the screen and the memory can handle. 最好的方法是仅加载屏幕和内存可以处理的内容。 The standard classes all are memory based and so you will reach the memory limit very early. 标准类都是基于内存的,因此您将尽早达到内存限制。 But there is OpenNetCF which is able to 'scale' images on the fly using the file stream only. 但是有OpenNetCF可以仅使用文件流即时缩放图像。

I used that in https://github.com/hjgode/eMdiMail/blob/master/ImagePanel/ImageHelper.cs to get scaled images that make sense to load and show. 我在https://github.com/hjgode/eMdiMail/blob/master/ImagePanel/ImageHelper.cs中使用了它,以获得可以加载和显示的缩放图像。 Look at the code for getScaledBitmap() and CreateThumbnail(). 查看getScaledBitmap()和CreateThumbnail()的代码。 These functions are part of a custom control in the code but can easily be used for other purposes. 这些功能是代码中自定义控件的一部分,但可以轻松地用于其他目的。

The project was used to scan documents or business cards, show them as thumbs and zoomed and then eMail the scanned images to a document server. 该项目用于扫描文档或名片,以拇指显示它们并进行缩放,然后将扫描的图像通过电子邮件发送到文档服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM