.net位图改变图像的颜色

时间:2014-12-30 15:17:15

标签: .net image crop

我试图在不丢失任何颜色的情况下调整图像大小。但我无法让这个工作。见下图,顶部是原版,底部是经过.NET的版本。

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

我尝试了很多不同的设置,寻找GDI +错误。更改调色板,将ImageAttributes更改为人们在互联网上建议的TileFlipXY。但似乎都没有效果。

enter image description here

我的代码:

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代码:

        ' 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 个答案:

答案 0 :(得分:2)

正如Hans Passant所说。我只需要在Image.FromFile(filename, True)

添加true

这是真的代表:

  

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

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