|
■No93597 (ももも さん) に返信
> やはり、うまくいきません。
当方では取得できているようなのですが、下記のコードだとどうなりますか?
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("全ての画素")
ComboBox1.Items.Add("Alpha = 0 の画素")
ComboBox1.Items.Add("Alpha = 127 の画素")
ComboBox1.Items.Add("Alpha = 255 の画素")
ComboBox1.Items.Add("0 < Alpha < 255 の画素")
ComboBox1.SelectedIndex = 4
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' http://uno036.starfree.jp/PRGmanual/ImagePng/house2-24A128.png
Dim srcFile As String = "C:\TEMP\透過50%.PNG"
Using srcImg As New Bitmap(srcFile)
Dim sz As New Size(46, 30)
Using dstImg As New Bitmap(sz.Height, sz.Width, PixelFormat.Format32bppArgb), g = Graphics.FromImage(dstImg)
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.DrawImage(srcImg, 0, 0, sz.Height, sz.Width)
Label1.Text = String.Format("{0}, {1}", srcImg.Size, srcImg.PixelFormat)
Label2.Text = String.Format("{0}, {1}", dstImg.Size, srcImg.PixelFormat)
Dim tbl1 = DumpBmp(srcImg)
Dim tbl2 = DumpBmp(dstImg)
Select Case ComboBox1.SelectedIndex
Case 1
DataGridView1.DataSource = tbl1.AsEnumerable().Where(Function(r) r.Field(Of Color)("Color").A = 0).AsDataView()
DataGridView2.DataSource = tbl2.AsEnumerable().Where(Function(r) r.Field(Of Color)("Color").A = 0).AsDataView()
Case 2
DataGridView1.DataSource = tbl1.AsEnumerable().Where(Function(r) r.Field(Of Color)("Color").A = 127).AsDataView()
DataGridView2.DataSource = tbl2.AsEnumerable().Where(Function(r) r.Field(Of Color)("Color").A = 127).AsDataView()
Case 3
DataGridView1.DataSource = tbl1.AsEnumerable().Where(Function(r) r.Field(Of Color)("Color").A = 255).AsDataView()
DataGridView2.DataSource = tbl2.AsEnumerable().Where(Function(r) r.Field(Of Color)("Color").A = 255).AsDataView()
Case 4
Dim ary() As Byte = {0, 255}
DataGridView1.DataSource = tbl1.AsEnumerable().Where(Function(r) Not ary.Contains(r.Field(Of Color)("Color").A)).AsDataView()
DataGridView2.DataSource = tbl2.AsEnumerable().Where(Function(r) Not ary.Contains(r.Field(Of Color)("Color").A)).AsDataView()
Case Else
DataGridView1.DataSource = tbl1
DataGridView2.DataSource = tbl2
End Select
DataGridView1.AutoResizeColumn(0)
DataGridView1.AutoResizeColumn(1)
DataGridView2.AutoResizeColumn(0)
DataGridView2.AutoResizeColumn(1)
End Using
End Using
End Sub
'Private Function DumpBmp(bmp As Bitmap) As DataTable
' Dim dt As New DataTable()
' Dim colX = dt.Columns.Add("X", GetType(Integer))
' Dim colY = dt.Columns.Add("Y", GetType(Integer))
' dt.Columns.Add("Color", GetType(Color)).AllowDBNull = False
' dt.PrimaryKey = New DataColumn() {colX, colY}
' '画素ごとに GetPixel を読み取って解析
' For y = 0 To bmp.Height - 1
' For x = 0 To bmp.Width - 1
' dt.Rows.Add(x, y, bmp.GetPixel(x, y))
' Next
' Next
' Return dt
'End Function
Private Function DumpBmp(bmp As Bitmap) As DataTable
Dim dt As New DataTable()
Dim colX = dt.Columns.Add("X", GetType(Integer))
Dim colY = dt.Columns.Add("Y", GetType(Integer))
dt.Columns.Add("Color", GetType(Color)).AllowDBNull = False
dt.PrimaryKey = New DataColumn() {colX, colY}
Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(Point.Empty, bmp.Size), ImageLockMode.ReadOnly, bmp.PixelFormat)
'まとめて Byte() に読み取ってから解析
Dim bin(bmpData.Stride * bmp.Height - 1) As Byte
Marshal.Copy(bmpData.Scan0, bin, 0, bin.Length)
For y = 0 To bmpData.Height - 1
For x = 0 To bmpData.Width - 1
Dim p As Integer = y * bmpData.Stride + x * 4
dt.Rows.Add(x, y, Color.FromArgb(bin(p + 3), bin(p + 2), bin(p + 1), bin(p + 0)))
Next
Next
bmp.UnlockBits(bmpData)
Return dt
End Function
'Private Function DumpBmp(bmp As Bitmap) As DataTable
' Dim dt As New DataTable()
' Dim colX = dt.Columns.Add("X", GetType(Integer))
' Dim colY = dt.Columns.Add("Y", GetType(Integer))
' dt.Columns.Add("Color", GetType(Color)).AllowDBNull = False
' dt.PrimaryKey = New DataColumn() {colX, colY}
' Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(Point.Empty, bmp.Size), ImageLockMode.ReadOnly, bmp.PixelFormat)
' '画素ごとに Scan0 のオフセットを読み取って解析
' For y = 0 To bmpData.Height - 1
' For x = 0 To bmpData.Width - 1
' Dim p As Integer = y * bmpData.Stride + x * 4
' dt.Rows.Add(x, y, Color.FromArgb(Marshal.ReadInt32(bmpData.Scan0, p)))
' Next
' Next
' bmp.UnlockBits(bmpData)
' Return dt
'End Function
End Class
|