.net位图更改图像的颜色 [英] .net Bitmap changes color of image

查看:75
本文介绍了.net位图更改图像的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调整图像大小而不丢失任何颜色.但是我无法使它正常工作.如下图所示,最上面的是原始的,最下面的是经过.NET的.

I am trying to resize the image without the loss of any color. But I can't get this to work. See the picture below, the top one is the original, the bottom one is the one which has gone through .NET.

我的问题是,如何保持颜色?

我尝试了许多不同的设置,以查找GDI +错误.更改调色板,将ImageAttributes更改为TileFlipXY,这是人们在Internet上建议的.但是似乎没有任何作用.

I have tried a lot of different settings, looking for GDI+ bugs. Changing the Palette, changing the ImageAttributes to TileFlipXY which people suggest on the internet. But none seems to work.

我的代码:

Public Class ImageEditor
    Private _img As Image
    Private _format As Imaging.ImageFormat
    Private _pixelformat As Imaging.PixelFormat
    Private _palette As Imaging.ColorPalette

    Public Sub New(ByVal img As Image)
        _img = img
        _palette = _img.Palette
        _format = _img.RawFormat
        _pixelformat = _img.PixelFormat
    End Sub

    Function getImage() As Image
        Return _img
    End Function

    ''' <summary>
    ''' Een stuk afbeelding uit een afbeelding knippen
    ''' </summary>
    ''' <param name="x1">Start X positie</param>
    ''' <param name="y1">Start Y positie</param>
    ''' <param name="width">Breedte van het stuk dat geknipt wordt</param>
    ''' <param name="height">Hoogte van het stuk dat geknipt wordt</param>
    ''' <remarks></remarks>
    Public Sub crop(ByVal x1 As Integer, ByVal y1 As Integer, ByVal width As Integer, ByVal height As Integer)
        ' Cropping the image
        Dim tmp As New Bitmap(width, height, _pixelformat)
        tmp.SetResolution(_img.HorizontalResolution, _img.VerticalResolution)

        Dim imageAttributes As New Imaging.ImageAttributes
        imageAttributes.SetWrapMode(Drawing2D.WrapMode.TileFlipXY)
        Using g = Graphics.FromImage(tmp)
            g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
            g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
            g.CompositingMode = Drawing2D.CompositingMode.SourceCopy

            g.DrawImage(_img, New Rectangle(0, 0, width, height), x1, y1, width, height, GraphicsUnit.Pixel, imageattributes)
        End Using
        _img = tmp
    End Sub

End Class

我的转换器类.任何我的.NET代码:

My converter class.. Any my .NET code:

        ' Load file
        Dim img As Image = Image.FromFile(filename)
        Dim format As Imaging.ImageFormat = img.RawFormat

        ' Crop and scale
        Dim editor As New ImageEditor(img)
        editor.crop(x1, y1, x2 - x1, y2 - y1)
        img.Dispose()
        ' Generate binairies
        Dim newImg As Image = editor.getImage()

        Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
        Dim myEncoderParameters As New EncoderParameters(1)
        Dim myEncoderParameter As New EncoderParameter(myEncoder, 100&)
        myEncoderParameters.Param(0) = myEncoderParameter

        Dim ms As New MemoryStream()
        newImg.Save(ms, GetEncoder(format), myEncoderParameters)
        Dim binairies As Byte() = ms.ToArray()

推荐答案

就像汉斯·帕森特(Hans Passant)所说的那样.我只需要在 Image.FromFile(filename,True)

It is as Hans Passant says. I just had to add the true at Image.FromFile(filename, True)

此真项代表:

使用该文件中嵌入的颜色管理信息从指定文件创建图像.

Creates an Image from the specified file using embedded color management information in that file.

更多信息: http://msdn.microsoft.com/zh-cn/library/system.drawing.image.fromfile%28v=vs.110%29.aspx

这篇关于.net位图更改图像的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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