Pesquisar

Criar Foto Sépia

Aqui está uma função que converte uma imagem ou bitmap em sépia.
Ele utiliza a primeira matriz para deixar a foto em preto e branco, e após ele aplica a matriz sépia sobre a foto.
Com a classe ColorMatrix, e Matrix podemos efetuar qualquer transformação de Cor e geométrica sobre a foto.

Private Function MontarImagemSepia(ByVal fotoUsuario As Bitmap) As Bitmap
Dim novoBitmap As New Bitmap(fotoUsuario.Width, fotoUsuario.Height)

Dim graphics As Graphics = System.Drawing.Graphics.FromImage(novoBitmap)

Dim matrixPB()() As Single = {New Single() {0.33, 0.33, 0.33, 0, 0}, _
New Single() {0.33, 0.33, 0.33, 0, 0}, _
New Single() {0.33, 0.33, 0.33, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}}

Dim matrixSepia()() As Single = {New Single() {0.439, 0.33, 0.33, 0, 0}, _
New Single() {0.33, 0.258, 0.33, 0, 0}, _
New Single() {0.33, 0.33, 0.078, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}}

Dim newColorMatrixPB As System.Drawing.Imaging.ColorMatrix = New System.Drawing.Imaging.ColorMatrix(matrixPB)
Dim newColorMatrixSepia As System.Drawing.Imaging.ColorMatrix = New System.Drawing.Imaging.ColorMatrix(matrixSepia)

Dim Attributes As System.Drawing.Imaging.ImageAttributes = New System.Drawing.Imaging.ImageAttributes()
Attributes.SetColorMatrix(newColorMatrixPB, Imaging.ColorMatrixFlag.Default, Imaging.ColorAdjustType.Bitmap)

graphics.DrawImage(fotoUsuario, New System.Drawing.Rectangle(0, 0, fotoUsuario.Width, fotoUsuario.Height), 0, 0, fotoUsuario.Width, fotoUsuario.Height, System.Drawing.GraphicsUnit.Pixel, Attributes)
graphics.Dispose()

Dim attributesSepia As System.Drawing.Imaging.ImageAttributes = New System.Drawing.Imaging.ImageAttributes()
attributesSepia.SetColorMatrix(newColorMatrixSepia, Imaging.ColorMatrixFlag.Default, Imaging.ColorAdjustType.Bitmap)

Dim newGraphicsSepia As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(novoBitmap)
newGraphicsSepia.DrawImage(novoBitmap, New System.Drawing.Rectangle(0, 0, novoBitmap.Width, novoBitmap.Height), 0, 0, novoBitmap.Width, novoBitmap.Height, System.Drawing.GraphicsUnit.Pixel, attributesSepia)
newGraphicsSepia.Dispose()

Return novoBitmap

End Function

Nenhum comentário: