T
tommaso.gastaldi
In a previous post I have been asking about a way to test Alpha
Transparency.
Bob and Michael have kindly provided some ideas.
Here I would like to share the function I have prepared, for the
purpose to improve it.
Frankly, I am not clear about the exact meaning of some pixel format
(max, gdi, etc.) and I hope I have put them under the right "case". I
have made only some superficial test and it seems to work.
Note that the purpose is to test ALPHA transparency (not transparency
in indexed-color image) this is useful because some programs do not
support it (as for instance IE 6)
I enclose all the source, included the handler for a button (button1)
on a form.
The logic I use is to make first a "preliminary quick check", based on
the consideration that, if Alpha transparency is used, in the 95% of
the cases it "shows up" around the picture. If the check is
undeterminate, the user can specify to perform a full scan (this needs
to be improved much).
Please, suggest all the code improvement that are useful to improve
this test and it's
speed ...
Imports System.Drawing.Imaging
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Dialog to load an image from disk
Dim FileImage As String
Dim Ofd As New OpenFileDialog
With Ofd
.Filter = "All files (*.*)|*.*" & _
"|Bitmap files (*.bmp)|*.bmp" & _
"|Portable Network Graphics files (*.png)|*.png"
& _
"|Jpeg files (*.jpg)|*.jpg" & _
"|Tiff files (*.tif)|*.tif" & _
"|GIF files (*.gif)|*.gif" & _
"|Windows Meta files (*.wmf)|*.wmf" & _
"|Windows Icon files (*.ico)|*.ico"
If Not .ShowDialog.Equals(DialogResult.OK) Then Exit Sub
FileImage = .FileName
.Dispose()
End With
Dim b As Bitmap
Dim i As Image
Try
i = Image.FromFile(FileImage)
Catch ex As Exception
MsgBox("Invalid picture or unsupported format")
Exit Sub
End Try
If TypeOf (i) Is Bitmap Then
b = DirectCast(i, Bitmap)
MsgBox("Uses Alpha transparency: " & _
Me.ImageUsesAlphaTransparency(b, True).ToString)
Else
MsgBox("Not a bitmap")
End If
End Sub
Public Function ImageUsesAlphaTransparency(ByVal Bitmap As Bitmap,
_
ByVal FullCheck As
Boolean) As Boolean
With Bitmap
Select Case .PixelFormat
Case PixelFormat.Indexed, _
PixelFormat.Format16bppGrayScale, _
PixelFormat.Format16bppRgb555, _
PixelFormat.Format16bppRgb565, _
PixelFormat.Format1bppIndexed, _
PixelFormat.Format24bppRgb, _
PixelFormat.Format32bppRgb, _
PixelFormat.Format48bppRgb, _
PixelFormat.Format4bppIndexed, _
PixelFormat.Format8bppIndexed, _
PixelFormat.Max '(what's that?)
Return False
Case PixelFormat.Format16bppArgb1555, _
PixelFormat.Format32bppArgb, _
PixelFormat.Format32bppPArgb, _
PixelFormat.Format64bppArgb, _
PixelFormat.Format64bppPArgb, _
PixelFormat.Gdi, _
PixelFormat.Alpha, _
PixelFormat.PAlpha, _
PixelFormat.DontCare, _
PixelFormat.Canonical, _
PixelFormat.Undefined, _
PixelFormat.Extended
'Quick check of the 4 corners and center
'(as, usually, transparency is used around a
picture)
If .GetPixel(0, 0).A < 255 Then Return True
If .GetPixel(Bitmap.Width - 1, Bitmap.Height - 1).A
< 255 Then Return True
If .GetPixel(0, Bitmap.Height - 1).A < 255 Then
Return True
If .GetPixel(Bitmap.Width - 1, 0).A < 255 Then
Return True
If .GetPixel(Bitmap.Width \ 2, Bitmap.Height \ 2).A
< 255 Then Return True
'further checking needed (suggest code improvement)
If FullCheck Then
For x As Integer = 0 To .Width - 1
For y As Integer = 0 To .Height - 1
If .GetPixel(x, y).A < 255 Then Return
True
Next y
Next x
Return False
Else
Return True 'conservative choice
End If
Case Else
MsgBox("Unexpected event")
Return True
End Select
End With
End Function
End Class
-tommaso
Transparency.
Bob and Michael have kindly provided some ideas.
Here I would like to share the function I have prepared, for the
purpose to improve it.
Frankly, I am not clear about the exact meaning of some pixel format
(max, gdi, etc.) and I hope I have put them under the right "case". I
have made only some superficial test and it seems to work.
Note that the purpose is to test ALPHA transparency (not transparency
in indexed-color image) this is useful because some programs do not
support it (as for instance IE 6)
I enclose all the source, included the handler for a button (button1)
on a form.
The logic I use is to make first a "preliminary quick check", based on
the consideration that, if Alpha transparency is used, in the 95% of
the cases it "shows up" around the picture. If the check is
undeterminate, the user can specify to perform a full scan (this needs
to be improved much).
Please, suggest all the code improvement that are useful to improve
this test and it's
speed ...
Imports System.Drawing.Imaging
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Dialog to load an image from disk
Dim FileImage As String
Dim Ofd As New OpenFileDialog
With Ofd
.Filter = "All files (*.*)|*.*" & _
"|Bitmap files (*.bmp)|*.bmp" & _
"|Portable Network Graphics files (*.png)|*.png"
& _
"|Jpeg files (*.jpg)|*.jpg" & _
"|Tiff files (*.tif)|*.tif" & _
"|GIF files (*.gif)|*.gif" & _
"|Windows Meta files (*.wmf)|*.wmf" & _
"|Windows Icon files (*.ico)|*.ico"
If Not .ShowDialog.Equals(DialogResult.OK) Then Exit Sub
FileImage = .FileName
.Dispose()
End With
Dim b As Bitmap
Dim i As Image
Try
i = Image.FromFile(FileImage)
Catch ex As Exception
MsgBox("Invalid picture or unsupported format")
Exit Sub
End Try
If TypeOf (i) Is Bitmap Then
b = DirectCast(i, Bitmap)
MsgBox("Uses Alpha transparency: " & _
Me.ImageUsesAlphaTransparency(b, True).ToString)
Else
MsgBox("Not a bitmap")
End If
End Sub
Public Function ImageUsesAlphaTransparency(ByVal Bitmap As Bitmap,
_
ByVal FullCheck As
Boolean) As Boolean
With Bitmap
Select Case .PixelFormat
Case PixelFormat.Indexed, _
PixelFormat.Format16bppGrayScale, _
PixelFormat.Format16bppRgb555, _
PixelFormat.Format16bppRgb565, _
PixelFormat.Format1bppIndexed, _
PixelFormat.Format24bppRgb, _
PixelFormat.Format32bppRgb, _
PixelFormat.Format48bppRgb, _
PixelFormat.Format4bppIndexed, _
PixelFormat.Format8bppIndexed, _
PixelFormat.Max '(what's that?)
Return False
Case PixelFormat.Format16bppArgb1555, _
PixelFormat.Format32bppArgb, _
PixelFormat.Format32bppPArgb, _
PixelFormat.Format64bppArgb, _
PixelFormat.Format64bppPArgb, _
PixelFormat.Gdi, _
PixelFormat.Alpha, _
PixelFormat.PAlpha, _
PixelFormat.DontCare, _
PixelFormat.Canonical, _
PixelFormat.Undefined, _
PixelFormat.Extended
'Quick check of the 4 corners and center
'(as, usually, transparency is used around a
picture)
If .GetPixel(0, 0).A < 255 Then Return True
If .GetPixel(Bitmap.Width - 1, Bitmap.Height - 1).A
< 255 Then Return True
If .GetPixel(0, Bitmap.Height - 1).A < 255 Then
Return True
If .GetPixel(Bitmap.Width - 1, 0).A < 255 Then
Return True
If .GetPixel(Bitmap.Width \ 2, Bitmap.Height \ 2).A
< 255 Then Return True
'further checking needed (suggest code improvement)
If FullCheck Then
For x As Integer = 0 To .Width - 1
For y As Integer = 0 To .Height - 1
If .GetPixel(x, y).A < 255 Then Return
True
Next y
Next x
Return False
Else
Return True 'conservative choice
End If
Case Else
MsgBox("Unexpected event")
Return True
End Select
End With
End Function
End Class
-tommaso