图像上的质量损失来自哪里? [英] Where does this quality loss on Images come from?

查看:134
本文介绍了图像上的质量损失来自哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Winforms应用程序中,它通过Linq连接到数据库到SQL我将图像(总是* .png)保存到一个如下所示的表:

in my Winforms application which is connected to a database via Linq to SQL I am saving images (always *.png) to a table which looks like this:

CREATE TABLE [dbo].[Images] (
    [Id]       INT   IDENTITY (1, 1) NOT NULL,
    [Bild]     IMAGE NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

在我可以存储图片之前,我必须将其转换为 byte [] 这就是我的工作方式:

Before I can store a picture I have to convert it to byte[] and this is how I do it:

public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
    using (MemoryStream ms = new MemoryStream())
    {
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        return ms.ToArray();
    }
}

之后如果我想加载这个相同的图像到我的应用程序中的PictureBox我用这种方法将其转换回来:

Afterwards if I want to load this very same image to a PictureBox in my application I convert it back with this method:

public static Image ByteArrayToImage(byte[] byteArrayIn)
{
    using (MemoryStream ms = new MemoryStream(byteArrayIn))
    {
        Image returnImage = Image.FromStream(ms);
        return returnImage;
    }
}

它确实有效,我尝试时出现的唯一问题在Picturebox中显示数据库中的图像。

It actually works, the only problem appears when I try to display an Image from the database in a Picturebox.

因此,当我将此Image加载到数据库时:

So when I load this Image to the database:

以后我试试显示它。它突然看起来像这样:

and later I try to display it. It suddenly looks like this:

我已经尝试了PictureBox的所有可能的SizeMode设置(Normal,Stretchimage,AutoSize,CenterImage,Zoom),它仍然看起来像这样。

I already tried all the possible SizeMode settings for the PictureBox (Normal, Stretchimage, AutoSize,CenterImage,Zoom) and it still looks like this.

这里也是我如何将图像从数据库加载到pictureBox:

Also here is how I load the Images from the database to the pictureBox:

首先我检索属于a的所有图像通过id设置:

First I retrieve the all the Images belonging to a set via id:

public static ImageList GetRezeptImages(int rezeptId) 
{
    using (CookBookDataContext ctx = new CookBookDataContext(ResourceFile.DBConnection))
    {
        IEnumerable<RezeptBilder> bilder = from b in ctx.RezeptBilders where b.FKRezept == rezeptId select b;
        ImageList imageList = new ImageList();

        foreach(RezeptBilder b in bilder)
        {
            imageList.Images.Add(Helper.ByteArrayToImage(b.Bild.ToArray()));
        }

        return imageList;                
    }
}

同样在我的应用程序中,我有一个datagridview,其中Id是存储在第一列中。因此,当我想要检索属于该集合的任何图像时,我会这样做:

Also in my application I have a datagridview where Id's are stored in the first column. So when I want to retrieve any Images belonging to the set I do it like this:

private void dgvRezeptListe_CellClick(object sender, DataGridViewCellEventArgs e)
{
    pbRezeptBild.Image = DBManager.GetRezeptImages(Int32.Parse(dgvRezeptListe.SelectedRows[0].Cells[0].Value.ToString())).Images[0];      
}

从本地目录加载时,Image在pictureBox中看起来很好。我还尝试将初始图片转换为二进制并返回(不加载到数据库),在pictureBox中显示它仍然看起来很好。

When loaded from a local directory the Image looks fine in the pictureBox. I also tried to convert the initial picture to binary and back (without loading it to the database), it still looked fine when displayed in the pictureBox.

还有其他的东西我在调试来自数据库的Image时意识到了。在查看ImageSize时,宽度和高度都具有值16.这很奇怪,因为原始图像具有完全不同的尺寸。

There is something else I realized while debugging the Image which came from the database. When looking into the ImageSize, width and height had both the value 16. This is weird because the original image has totally different dimensions.

任何想法?

推荐答案

c> ImageList 似乎是在将图像添加到MemoryBmp时将其转换为MemoryBmp code> GetRezeptImages 到列表中。

我不知道为什么会这样做,但这就是你在图像质量下降的原因。正如您还意识到的那样,图像被转换为​​16x16图像,然后当您在 PictureBox 中将其调整为原始大小时,它看起来更加俗气。

It appears that ImageList is converting your images to MemoryBmp's when you are adding them in GetRezeptImages to the list.
I am not aware as to why it would do that, but that is the reason for your loss of quality in the image. As you also realised, the image is converted to a 16x16 image and then when that is resized in your PictureBox to the original size, it just looks more cheesy.

编辑:

来自TaW的评论:ImageList集合无法处理不同大小的图像,因此它将所有图像转换为公共图像尺寸。由于没有设置大小,它似乎默认为16x16。


From TaW's comment: The ImageList collection cannot handle varying sized images, so it is converting all the images to a common size. Since there is no size set, it seems it defaults to 16x16.

我建议您更改 GetRezeptImages 返回列表< Image> 而不是 ImageList 的方法,并相应地使用它来显示图像。

I would recommend that you change the GetRezeptImages method to return a List<Image> instead of an ImageList and use that accordingly to show the images.

或者,如果您将始终使用 GetRezeptImages 方法,就像在问题中显示的那样,您可以更改它总是只返回 Image 对象中的第一个图像,并完全扔掉所有列表。

Alternatively, if you will always use the GetRezeptImages method in the same way that you showed in your question, you can change it to always just return the first image in an Image object and entirely throw away all the lists.

这篇关于图像上的质量损失来自哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆