62,244
社区成员




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace QRCodeTransfer
{
public partial class Form1 : Form
{
private Bitmap raw_image = (Bitmap)Image.FromFile("raw_code.png");
private Bitmap target_image = null;
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(raw_image, 0, 0, panel1.Width, panel1.Height);
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
if(target_image != null)
{
e.Graphics.DrawImage(target_image, 0, 0, panel2.Width, panel2.Height);
}
}
private void button1_Click(object sender, EventArgs e)
{
//1.计算最小宽度
target_image = new Bitmap(raw_image.Width, raw_image.Height);
int x = raw_image.Width / 2;
Rectangle center_box=new Rectangle();
Color center_color = raw_image.GetPixel(x, x);
int box_left = 0;
int box_right = 0;
while (x>0)
{
if(raw_image.GetPixel(x,x)!=center_color)
{
box_left = x+1;
break;
}
x--;
}
x = raw_image.Width / 2;
while (x < raw_image.Width)
{
if (raw_image.GetPixel(x, x) != center_color)
{
box_right = x - 1;
break;
}
x++;
}
int a = box_right - box_left + 1;
center_box = new Rectangle(box_left, box_left, a,a);
//2.绘制新二维码
using (Graphics g = Graphics.FromImage(target_image))
{
g.Clear(Color.White);
int box_count = raw_image.Width / center_box.Width;
for (int i = 0; i < box_count; i++)
{
for (int j = 0; j < box_count; j++)
{
if (raw_image.GetPixel(i * center_box.Width, j * center_box.Height) == center_color)
{
Rectangle box = new Rectangle(i * center_box.Width, j * center_box.Height, center_box.Width, center_box.Height);
g.FillEllipse(Brushes.Black, box);
}
}
}
}
target_image.Save("target.png", System.Drawing.Imaging.ImageFormat.Png);
//3.更新界面
panel2.Invalidate();
}
}
}