.net Bitmap changes color of image

心不动则不痛 提交于 2020-01-30 09:44:07

问题


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.

My question is, how can I keep the colors?

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.

My Code:

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

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()

回答1:


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

This true stands for:

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

More info: http://msdn.microsoft.com/en-us/library/system.drawing.image.fromfile%28v=vs.110%29.aspx



来源:https://stackoverflow.com/questions/27707965/net-bitmap-changes-color-of-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!