Public Class Form1
Inherits System.Windows.Forms.Form#Region " Windows Form Designer generated code " Public Sub New()
MyBase.New() 'This call is required by the Windows Form Designer.
InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Progress As System.Windows.Forms.ProgressBar
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Button1 = New System.Windows.Forms.Button
Me.Progress = New System.Windows.Forms.ProgressBar
Me.Label1 = New System.Windows.Forms.Label
Me.PictureBox2 = New System.Windows.Forms.PictureBox
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
Me.PictureBox1.Location = New System.Drawing.Point(8, 8)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(160, 160)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 176)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(328, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "&Apply"
'
'Progress
'
Me.Progress.Location = New System.Drawing.Point(8, 224)
Me.Progress.Name = "Progress"
Me.Progress.Size = New System.Drawing.Size(328, 23)
Me.Progress.TabIndex = 2
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 208)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(264, 23)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Pass 1 of 2:"
'
'PictureBox2
'
Me.PictureBox2.Image = CType(resources.GetObject("PictureBox2.Image"), System.Drawing.Image)
Me.PictureBox2.Location = New System.Drawing.Point(176, 8)
Me.PictureBox2.Name = "PictureBox2"
Me.PictureBox2.Size = New System.Drawing.Size(160, 160)
Me.PictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage
Me.PictureBox2.TabIndex = 4
Me.PictureBox2.TabStop = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(344, 254)
Me.Controls.Add(Me.PictureBox2)
Me.Controls.Add(Me.Progress)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PictureBox1)
Me.Name = "Form1"
Me.Text = "Blur"
Me.ResumeLayout(False) End Sub#End Region Private Function Average(ByVal Size As Size, ByVal imageSize As SizeF, ByVal PixelX As Integer, ByVal Pixely As Integer) As Color
Dim pixels As New ArrayList
Dim x As Integer, y As Integer
Dim bmp As Bitmap = PictureBox1.Image.Clone ' Find the color for each pixel and add it to a new array.
'
' Remember a 5X5 area on or near the edge will ask for pixels that don't
' exist in our image, this will filter those out.
'
For x = PixelX - CInt(Size.Width / 2) To PixelX + CInt(Size.Width / 2)
For y = Pixely - CInt(Size.Height / 2) To Pixely + CInt(Size.Height / 2)
If (x > 0 And x < imageSize.Width) And _
(y > 0 And y < imageSize.Height) Then
pixels.Add(bmp.GetPixel(x, y))
End If
Next
Next ' Adverage the A, R, G, B channels
' reflected in the array
Dim thisColor As Color Dim alpha As Integer = 0
Dim red As Integer = 0
Dim green As Integer = 0
Dim blue As Integer = 0 For Each thisColor In pixels
alpha += thisColor.A
red += thisColor.R
green += thisColor.G
blue += thisColor.B
Next ' Return the sum of the colors / the number of colors (The average)
'
Return Color.FromArgb(alpha / pixels.Count, _
red / pixels.Count, _
green / pixels.Count, _
blue / pixels.Count)
End Function Private Sub gausianBlur(ByVal alphaEdgesOnly As Boolean, ByVal blurSize As Size)
Dim PixelY As Integer
Dim PixelX As Integer
Dim bmp As Bitmap = PictureBox1.Image.Clone ' UI Stuff
Label1.Text = "Applying Gausian Blur of " & blurSize.ToString Progress.Maximum = bmp.Height * bmp.Width
Progress.Minimum = 0
Progress.Value = 0 ' Loop the rows of the image
For PixelY = 0 To bmp.Width - 1 ' Loop the cols of the image
For PixelX = 0 To bmp.Height - 1
If Not alphaEdgesOnly Then
bmp.SetPixel(PixelX, PixelY, Average(blurSize, bmp.PhysicalDimension, PixelX, PixelY))
ElseIf bmp.GetPixel(PixelX, PixelY).A <> 255 Then
bmp.SetPixel(PixelX, PixelY, Average(blurSize, bmp.PhysicalDimension, PixelX, PixelY))
End If
Progress.Value += 1
Application.DoEvents()
Next
Next PictureBox1.Image = bmp.Clone
bmp.Dispose()
End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
gausianBlur(False, New Size(6, 6))
End SubEnd Class这个如何转为C#的。我被搞糊涂了。
Inherits System.Windows.Forms.Form#Region " Windows Form Designer generated code " Public Sub New()
MyBase.New() 'This call is required by the Windows Form Designer.
InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Progress As System.Windows.Forms.ProgressBar
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Button1 = New System.Windows.Forms.Button
Me.Progress = New System.Windows.Forms.ProgressBar
Me.Label1 = New System.Windows.Forms.Label
Me.PictureBox2 = New System.Windows.Forms.PictureBox
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
Me.PictureBox1.Location = New System.Drawing.Point(8, 8)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(160, 160)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 176)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(328, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "&Apply"
'
'Progress
'
Me.Progress.Location = New System.Drawing.Point(8, 224)
Me.Progress.Name = "Progress"
Me.Progress.Size = New System.Drawing.Size(328, 23)
Me.Progress.TabIndex = 2
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 208)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(264, 23)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Pass 1 of 2:"
'
'PictureBox2
'
Me.PictureBox2.Image = CType(resources.GetObject("PictureBox2.Image"), System.Drawing.Image)
Me.PictureBox2.Location = New System.Drawing.Point(176, 8)
Me.PictureBox2.Name = "PictureBox2"
Me.PictureBox2.Size = New System.Drawing.Size(160, 160)
Me.PictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage
Me.PictureBox2.TabIndex = 4
Me.PictureBox2.TabStop = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(344, 254)
Me.Controls.Add(Me.PictureBox2)
Me.Controls.Add(Me.Progress)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PictureBox1)
Me.Name = "Form1"
Me.Text = "Blur"
Me.ResumeLayout(False) End Sub#End Region Private Function Average(ByVal Size As Size, ByVal imageSize As SizeF, ByVal PixelX As Integer, ByVal Pixely As Integer) As Color
Dim pixels As New ArrayList
Dim x As Integer, y As Integer
Dim bmp As Bitmap = PictureBox1.Image.Clone ' Find the color for each pixel and add it to a new array.
'
' Remember a 5X5 area on or near the edge will ask for pixels that don't
' exist in our image, this will filter those out.
'
For x = PixelX - CInt(Size.Width / 2) To PixelX + CInt(Size.Width / 2)
For y = Pixely - CInt(Size.Height / 2) To Pixely + CInt(Size.Height / 2)
If (x > 0 And x < imageSize.Width) And _
(y > 0 And y < imageSize.Height) Then
pixels.Add(bmp.GetPixel(x, y))
End If
Next
Next ' Adverage the A, R, G, B channels
' reflected in the array
Dim thisColor As Color Dim alpha As Integer = 0
Dim red As Integer = 0
Dim green As Integer = 0
Dim blue As Integer = 0 For Each thisColor In pixels
alpha += thisColor.A
red += thisColor.R
green += thisColor.G
blue += thisColor.B
Next ' Return the sum of the colors / the number of colors (The average)
'
Return Color.FromArgb(alpha / pixels.Count, _
red / pixels.Count, _
green / pixels.Count, _
blue / pixels.Count)
End Function Private Sub gausianBlur(ByVal alphaEdgesOnly As Boolean, ByVal blurSize As Size)
Dim PixelY As Integer
Dim PixelX As Integer
Dim bmp As Bitmap = PictureBox1.Image.Clone ' UI Stuff
Label1.Text = "Applying Gausian Blur of " & blurSize.ToString Progress.Maximum = bmp.Height * bmp.Width
Progress.Minimum = 0
Progress.Value = 0 ' Loop the rows of the image
For PixelY = 0 To bmp.Width - 1 ' Loop the cols of the image
For PixelX = 0 To bmp.Height - 1
If Not alphaEdgesOnly Then
bmp.SetPixel(PixelX, PixelY, Average(blurSize, bmp.PhysicalDimension, PixelX, PixelY))
ElseIf bmp.GetPixel(PixelX, PixelY).A <> 255 Then
bmp.SetPixel(PixelX, PixelY, Average(blurSize, bmp.PhysicalDimension, PixelX, PixelY))
End If
Progress.Value += 1
Application.DoEvents()
Next
Next PictureBox1.Image = bmp.Clone
bmp.Dispose()
End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
gausianBlur(False, New Size(6, 6))
End SubEnd Class这个如何转为C#的。我被搞糊涂了。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Blur
{
public class Form1 : System.Windows.Forms.Form
{ private System.ComponentModel.IContainer components;
internal System.Windows.Forms.PictureBox PictureBox1;
internal System.Windows.Forms.Button Button1;
internal System.Windows.Forms.ProgressBar Progress;
internal System.Windows.Forms.Label Label1;
internal System.Windows.Forms.PictureBox PictureBox2; /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} public Form1()
{
//base.New();
InitializeComponent();
} protected override void Dispose(bool disposing)
{
if (disposing)
{
if (!(components == null))
{
components.Dispose();
}
}
base.Dispose(disposing);
} [System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.PictureBox1 = new System.Windows.Forms.PictureBox();
this.Button1 = new System.Windows.Forms.Button();
this.Progress = new System.Windows.Forms.ProgressBar();
this.Label1 = new System.Windows.Forms.Label();
this.PictureBox2 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// PictureBox1
//
this.PictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("PictureBox1.Image")));
this.PictureBox1.Location = new System.Drawing.Point(10, 9);
this.PictureBox1.Name = "PictureBox1";
this.PictureBox1.Size = new System.Drawing.Size(192, 172);
this.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.PictureBox1.TabIndex = 0;
this.PictureBox1.TabStop = false;
//
// Button1
//
this.Button1.Location = new System.Drawing.Point(10, 190);
this.Button1.Name = "Button1";
this.Button1.Size = new System.Drawing.Size(393, 24);
this.Button1.TabIndex = 1;
this.Button1.Text = "&Apply";
this.Button1.Click += new System.EventHandler(this.Button1_Click);
//
// Progress
//
this.Progress.Location = new System.Drawing.Point(10, 241);
this.Progress.Name = "Progress";
this.Progress.Size = new System.Drawing.Size(393, 25);
this.Progress.TabIndex = 2;
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(10, 224);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(316, 25);
this.Label1.TabIndex = 3;
this.Label1.Text = "Pass 1 of 2:";
//
// PictureBox2
//
this.PictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("PictureBox2.Image")));
this.PictureBox2.Location = new System.Drawing.Point(211, 9);
this.PictureBox2.Name = "PictureBox2";
this.PictureBox2.Size = new System.Drawing.Size(192, 172);
this.PictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.PictureBox2.TabIndex = 4;
this.PictureBox2.TabStop = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(412, 273);
this.Controls.Add(this.PictureBox2);
this.Controls.Add(this.Progress);
this.Controls.Add(this.Label1);
this.Controls.Add(this.Button1);
this.Controls.Add(this.PictureBox1);
this.Name = "Form1";
this.Text = "Blur";
this.ResumeLayout(false); } private Color Average(Size Size, SizeF imageSize, int PixelX, int Pixely)
{
ArrayList pixels = new ArrayList();
//int x;
//int y;
Bitmap bmp = (Bitmap) PictureBox1.Image.Clone();
for (int x = PixelX - System.Convert.ToInt32(Size.Width / 2); x <= PixelX + System.Convert.ToInt32(Size.Width / 2); x++)
{
for (int y = Pixely - System.Convert.ToInt32(Size.Height / 2); y <= Pixely + System.Convert.ToInt32(Size.Height / 2); y++)
{
if ((x > 0 & x < imageSize.Width) & (y > 0 & y < imageSize.Height))
{
pixels.Add(bmp.GetPixel(x, y));
}
}
}
//Color thisColor;
int alpha = 0;
int red = 0;
int green = 0;
int blue = 0;
foreach (Color thisColor in pixels)
{
alpha += thisColor.A;
red += thisColor.R;
green += thisColor.G;
blue += thisColor.B;
}
return Color.FromArgb(alpha / pixels.Count, red / pixels.Count, green / pixels.Count, blue / pixels.Count);
} private void gausianBlur(bool alphaEdgesOnly, Size blurSize)
{
// int PixelY;
// int PixelX;
Bitmap bmp = (Bitmap) PictureBox1.Image.Clone();
Label1.Text = "Applying Gausian Blur of " + blurSize.ToString();
Progress.Maximum = bmp.Height * bmp.Width;
Progress.Minimum = 0;
Progress.Value = 0;
for (int PixelY = 0; PixelY <= bmp.Width - 1; PixelY++)
{
for (int PixelX = 0; PixelX <= bmp.Height - 1; PixelX++)
{
if (!alphaEdgesOnly)
{
bmp.SetPixel(PixelX, PixelY, Average(blurSize, bmp.PhysicalDimension, PixelX, PixelY)); //就是这里出问题了:报使用了无效的参数,调试信息显示PixelX=0;PixelY=300;blurSize为6*6;bmp.PhysicalDimension为300*400;
}
else if (bmp.GetPixel(PixelX, PixelY).A != 255)
{
bmp.SetPixel(PixelX, PixelY, Average(blurSize, bmp.PhysicalDimension, PixelX, PixelY));
}
Progress.Value += 1;
Application.DoEvents();
}
}
PictureBox1.Image =(Image) bmp.Clone();
bmp.Dispose();
} private void Button1_Click(object sender, System.EventArgs e)
{
gausianBlur(false, new Size(6, 6));
}
}
}就是这行出调试错误了:
bmp.SetPixel(PixelX, PixelY, Average(blurSize, bmp.PhysicalDimension, PixelX, PixelY)); //就是这里出问题了:报使用了无效的参数,调试信息显示PixelX=0;PixelY=300;blurSize为6*6;bmp.PhysicalDimension为300*400;
哪位大侠解决一下?