clsAPIImageUtils.vb · 46494 B · ext
.vb · seen 427× in SWHvia fallback
swh:1:cnt:a460be856393566b65083d9a69a8f2b1b9f5470e;origin=https://github.com/sig-rnd-io-testuser/100MbwithMalware;anchor=swh:1:rev:b9316b3a397bdc57fd0b73db661950584e4f918b;path=/EmberAPI/clsAPIImageUtils.vb
Show source
' ################################################################################
' # EMBER MEDIA MANAGER #
' ################################################################################
' ################################################################################
' # This file is part of Ember Media Manager. #
' # #
' # Ember Media Manager is free software: you can redistribute it and/or modify #
' # it under the terms of the GNU General Public License as published by #
' # the Free Software Foundation, either version 3 of the License, or #
' # (at your option) any later version. #
' # #
' # Ember Media Manager is distributed in the hope that it will be useful, #
' # but WITHOUT ANY WARRANTY; without even the implied warranty of #
' # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
' # GNU General Public License for more details. #
' # #
' # You should have received a copy of the GNU General Public License #
' # along with Ember Media Manager. If not, see <http://www.gnu.org/licenses/>. #
' ################################################################################
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.CompilerServices
Imports NLog
<Assembly: InternalsVisibleTo("EmberAPI_Test")>
Public Class ImageUtils
#Region "Fields"
Shared logger As Logger = LogManager.GetCurrentClassLogger()
Public Const DefaultPaddingARGB As Integer = -16777216 'This is FF000000, which is completely opaque black
#End Region 'Fields
#Region "Methods"
''' <summary>
''' Adds an overlay on the provided image to indicate that it is a "DUPLICATE"
''' </summary>
''' <param name="oImage">Source <c>Images</c></param>
''' <returns><c>Image</c> with "DUPLICATE" overlay, or just the source <paramref name="oImage"/> if an error was encountered.</returns>
''' <remarks></remarks>
Public Shared Function AddDuplicateStamp(ByVal oImage As Image) As Image
If oImage Is Nothing Then Return oImage
Using sImage As New Bitmap(oImage)
'now overlay "DUPLICATE" image
Using grOverlay As Graphics = Graphics.FromImage(sImage)
Dim oWidth As Integer = If(sImage.Width >= Image.FromFile(FileUtils.Common.ReturnSettingsFile("Images\Defaults", "Duplicate.png")).Width, Image.FromFile(FileUtils.Common.ReturnSettingsFile("Images\Defaults", "Duplicate.png")).Width, sImage.Width)
Dim oheight As Integer = If(sImage.Height >= Image.FromFile(FileUtils.Common.ReturnSettingsFile("Images\Defaults", "Duplicate.png")).Height, Image.FromFile(FileUtils.Common.ReturnSettingsFile("Images\Defaults", "Duplicate.png")).Height, sImage.Height)
grOverlay.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
grOverlay.DrawImage(Image.FromFile(FileUtils.Common.ReturnSettingsFile("Images\Defaults", "Duplicate.png")), 0, 0, oWidth, oheight)
End Using
Dim nImage As New Images
nImage.UpdateMSfromImg(sImage)
Return nImage.Image
End Using
End Function
''' <summary>
''' Adds an overlay on the provided image to indicate that it is "missing"
''' </summary>
''' <param name="oImage">Source <c>Images</c></param>
''' <returns><c>Images</c> with "missing" overlay, or just the source <paramref name="oImage"/> if an error was encountered.</returns>
''' <remarks></remarks>
Public Shared Function AddMissingStamp(ByVal oImage As Images) As Images
If oImage Is Nothing OrElse oImage.Image Is Nothing Then Return oImage
Using nImage As New Bitmap(GrayScale(oImage).Image)
'now overlay "missing" image
Using grOverlay As Graphics = Graphics.FromImage(nImage)
Dim oWidth As Integer = If(nImage.Width >= Image.FromFile(FileUtils.Common.ReturnSettingsFile("Images\Defaults", "Missing.png")).Width, Image.FromFile(FileUtils.Common.ReturnSettingsFile("Images\Defaults", "Missing.png")).Width, nImage.Width)
Dim oheight As Integer = If(nImage.Height >= Image.FromFile(FileUtils.Common.ReturnSettingsFile("Images\Defaults", "Missing.png")).Height, Image.FromFile(FileUtils.Common.ReturnSettingsFile("Images\Defaults", "Missing.png")).Height, nImage.Height)
grOverlay.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
grOverlay.DrawImage(Image.FromFile(FileUtils.Common.ReturnSettingsFile("Images\Defaults", "Missing.png")), 0, 0, oWidth, oheight)
End Using
oImage.UpdateMSfromImg(nImage)
End Using
Return oImage
End Function
''' <summary>
''' Converts the source <paramref name="oImage"/> to grayscale
''' </summary>
''' <param name="oImage">Source <c>Images</c></param>
''' <returns><c>Images</c> that has been converted to grayscale, or just the source <paramref name="oImage"/> if an error was encountered.</returns>
''' <remarks></remarks>
Public Shared Function GrayScale(ByVal oImage As Images) As Images
If oImage Is Nothing OrElse oImage.Image Is Nothing Then Return oImage
Dim nImage As New Bitmap(oImage.Image)
'first let's convert the background to grayscale
Using g As Graphics = Graphics.FromImage(nImage)
Dim cm As Imaging.ColorMatrix = New Imaging.ColorMatrix(New Single()() _
{New Single() {0.5, 0.5, 0.5, 0, 0},
New Single() {0.5, 0.5, 0.5, 0, 0},
New Single() {0.5, 0.5, 0.5, 0, 0},
New Single() {0, 0, 0, 1, 0},
New Single() {0, 0, 0, 0, 1}})
Dim ia As Imaging.ImageAttributes = New Imaging.ImageAttributes()
ia.SetColorMatrix(cm)
g.DrawImage(nImage, New Rectangle(0, 0, nImage.Width, nImage.Height), 0, 0, nImage.Width, nImage.Height, GraphicsUnit.Pixel, ia)
End Using
oImage.UpdateMSfromImg(nImage)
Return oImage
End Function
''' <summary>
''' Draw a gradiated ellipse on the supplied <paramref name="graphics"/>, defined by <paramref name="bounds"/>,
''' with a line color of <paramref name="outerColor"/> and a center/fill of <paramref name="centerColor"/>.
''' </summary>
''' <param name="graphics"><c>Graphics</c> surface to draw on</param>
''' <param name="bounds"><c>Rectangle</c> that defines the boundaries of the ellipse</param>
''' <param name="centerColor">The center <c>Color</c></param>
''' <param name="outerColor">The outer edge <c>Color</c></param>
''' <remarks></remarks>
Public Shared Sub DrawGradEllipse(ByRef graphics As Graphics, ByVal bounds As Rectangle, ByVal centerColor As Color, ByVal outerColor As Color)
'Some quick-and-dirty sanity checking
If graphics Is Nothing OrElse (bounds.Width = 0 And bounds.Height = 0) Then Return
Try
Using gPath As New Drawing2D.GraphicsPath
gPath.AddEllipse(bounds.X, bounds.Y, bounds.Width, bounds.Height)
Using pgBrush = New Drawing2D.PathGradientBrush(gPath)
pgBrush.CenterColor = centerColor
pgBrush.SurroundColors = New Color() {outerColor, outerColor, outerColor, outerColor}
graphics.FillEllipse(pgBrush, bounds.X, bounds.Y, bounds.Width, bounds.Height)
End Using
End Using
Catch ex As Exception
logger.Error(ex, New StackFrame().GetMethod().Name)
End Try
End Sub
''' <summary>
''' Resize the supplied <paramref name="_image"/>, preserving proportions.
''' </summary>
''' <param name="_image">Source <c>Image</c> to manipulate</param>
''' <param name="maxWidth">Maximum width of resized image</param>
''' <param name="maxHeight">Maximum height of resized image</param>
''' <param name="usePadding">If <c>True</c>, background of bounding rectangle not covered by the
''' resized image will be drawn with <paramref name="PaddingARGB"/> color</param>
''' <param name="PaddingARGB">Optional color to use as padding.</param>
''' <remarks>Image will retain the same proportions, however will resize such that it can fit
''' within the <paramref name="maxWidth"/> and <paramref name="maxHeight"/>. If <paramref name="usePadding"/> is specified,
''' the background of the rectangle specified by <paramref name="maxWidth"/> and <paramref name="maxHeight"/> will be filled
''' by <paramref name="PaddingARGB"/></remarks>
Public Shared Sub ResizeImage(ByRef _image As Image, ByVal maxWidth As Integer, ByVal maxHeight As Integer, Optional ByVal usePadding As Boolean = False, Optional ByVal PaddingARGB As Integer = DefaultPaddingARGB)
If (_image Is Nothing) OrElse
(maxWidth <= 0) OrElse
(maxHeight <= 0) Then
Return
End If
Try
Dim sPropPerc As Single = 1.0 'no default scaling
If _image.Width > _image.Height Then
sPropPerc = CSng(maxWidth / _image.Width)
Else
sPropPerc = CSng(maxHeight / _image.Height)
End If
' Get the source bitmap.
Using bmSource As New Bitmap(_image)
' Make a bitmap for the result.
Dim bmDest As New Bitmap(
Convert.ToInt32(bmSource.Width * sPropPerc),
Convert.ToInt32(bmSource.Height * sPropPerc))
' Make a Graphics object for the result Bitmap.
Using grDest As Graphics = Graphics.FromImage(bmDest)
grDest.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
' Copy the source image into the destination bitmap.
grDest.DrawImage(bmSource, New Rectangle(0, 0,
bmDest.Width, bmDest.Height), New Rectangle(0, 0,
bmSource.Width, bmSource.Height), GraphicsUnit.Pixel)
End Using
If usePadding Then
Dim bgBMP As Bitmap = New Bitmap(maxWidth, maxHeight)
Using grOverlay As Graphics = Graphics.FromImage(bgBMP)
grOverlay.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
grOverlay.FillRectangle(New SolidBrush(Color.FromArgb(PaddingARGB)), New RectangleF(0, 0, maxWidth, maxHeight))
Dim iLeft As Integer = Convert.ToInt32(If(bmDest.Width = maxWidth, 0, (maxWidth - bmDest.Width) / 2))
Dim iTop As Integer = Convert.ToInt32(If(bmDest.Height = maxHeight, 0, (maxHeight - bmDest.Height) / 2))
grOverlay.DrawImage(bmDest, iLeft, iTop, bmDest.Width, bmDest.Height)
End Using
bmDest = bgBMP
End If
_image = bmDest
End Using
Catch ex As Exception
logger.Error(ex, New StackFrame().GetMethod().Name)
End Try
End Sub
''' <summary>
''' Copies the image from <paramref name="pbSource"/> into <paramref name="pbDestination"/>,
''' resizing it (if necessary) so that it is no larger than the supplied width and height.
''' It only shrinks, but does not grow the image.
'''
''' </summary>
''' <param name="pbDestination">Destination picture box</param>
''' <param name="pbSource">Source picture box</param>
''' <param name="maxHeight">Maximum height for <paramref name="pbDestination"/></param>
''' <param name="maxWidth">Maximum width for <paramref name="pbDestination"/></param>
''' <remarks>Why not use "Zoom" for fanart background? - To keep the image at the top. Zoom centers vertically.</remarks>
Public Shared Sub ResizePB(ByRef pbDestination As PictureBox, ByRef pbSource As PictureBox, ByVal maxHeight As Integer, ByVal maxWidth As Integer)
If pbSource Is Nothing OrElse pbSource.Image Is Nothing Then Return
If pbSource.Image IsNot Nothing Then
Try
If Not pbDestination.Image Is Nothing Then pbDestination.Image.Dispose()
pbDestination.SizeMode = PictureBoxSizeMode.Normal
Dim sPropPerc As Single = 1.0 'no default scaling
pbDestination.Size = New Size(maxWidth, maxHeight)
' Height
If pbSource.Image.Height > pbDestination.Height Then
' Reduce height first
sPropPerc = CSng(pbDestination.Height / pbSource.Image.Height)
End If
' Width
If (pbSource.Image.Width * sPropPerc) > pbDestination.Width Then
' Scaled width exceeds Box's width, recalculate scale_factor
sPropPerc = CSng(pbDestination.Width / pbSource.Image.Width)
End If
' Get the source bitmap.
Using bmSource As New Bitmap(pbSource.Image)
' Make a bitmap for the result.
Dim bmDest As New Bitmap(
Convert.ToInt32(bmSource.Width * sPropPerc),
Convert.ToInt32(bmSource.Height * sPropPerc))
' Make a Graphics object for the result Bitmap.
Using grDest As Graphics = Graphics.FromImage(bmDest)
' Copy the source image into the destination bitmap.
grDest.DrawImage(bmSource, 0, 0,
bmDest.Width + 1,
bmDest.Height + 1)
' Display the result.
pbDestination.Image = bmDest
'tweak pb after resizing pic
pbDestination.Width = pbDestination.Image.Width
pbDestination.Height = pbDestination.Image.Height
'Clean up
bmDest = Nothing
End Using
End Using
Catch ex As Exception
pbDestination.Left = 0
pbDestination.Size = New Size(maxWidth, maxHeight)
logger.Error(ex, New StackFrame().GetMethod().Name)
End Try
Else
pbDestination.Left = 0
pbDestination.Size = New Size(maxWidth, maxHeight)
End If
End Sub
''' <summary>
''' Apply a glass overlay on the supplied <c>PictureBox</c>
''' </summary>
''' <param name="pbUnderlay"><c>PictureBox</c> representing the source image</param>
''' <remarks></remarks>
Public Shared Sub SetGlassOverlay(ByRef pbUnderlay As PictureBox)
If (pbUnderlay Is Nothing) OrElse (pbUnderlay.Image Is Nothing) Then Return
Try
Dim bmOverlay As New Bitmap(pbUnderlay.Image)
Using grOverlay As Graphics = Graphics.FromImage(bmOverlay)
Dim bmHeight As Integer = Convert.ToInt32(pbUnderlay.Image.Height * 0.65)
grOverlay.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
grOverlay.DrawImage(Image.FromFile(FileUtils.Common.ReturnSettingsFile("Images\Defaults", "Overlay.png")), 0, 0, pbUnderlay.Image.Width, bmHeight)
pbUnderlay.Image = bmOverlay
bmOverlay = New Bitmap(pbUnderlay.Image)
End Using
Using grOverlay = Graphics.FromImage(bmOverlay)
grOverlay.DrawImage(Image.FromFile(FileUtils.Co
…(truncated)…