代码拉取完成,页面将自动刷新
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace BitmapPtr
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string path = $"{Application.StartupPath}{Path.DirectorySeparatorChar}test.png";
if (File.Exists(path))
TbxFile.Text = path;
}
private void BtnOpen_Click(object sender, EventArgs e)
{
string file;
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = Application.StartupPath + Path.DirectorySeparatorChar;
dialog.Title = "请选择文件";
dialog.Filter = "图片文件(*.jpg,*.png,*.bmp,*.tif)|*.jpg;*.png;*.bmp;*.tif";
if (dialog.ShowDialog() == DialogResult.OK)
{
file = dialog.FileName;
TbxFile.Text = file;
BtnTest_Click(null,null);
}
}
private void BtnTest_Click(object sender, EventArgs e)
{
if (TbxFile.Text.Length==0 || !File.Exists(TbxFile.Text))
{
MessageBox.Show("请先选择文件");
return;
}
HiPerfTimer timer = new HiPerfTimer();
using (Bitmap bmp = (Bitmap)Image.FromFile(TbxFile.Text))
{
RtbMsg.AppendText($"Bitmap文件名:{Path.GetFileName(TbxFile.Text)},图像尺寸{bmp.Width}*{bmp.Height},图像位数{bmp.PixelFormat}{Environment.NewLine}");
timer.Start();
byte[] bmpbyte = BitmapHelper.Bitmap2Byte(bmp,out int width,out int height,
out int stride, out int depth,out PixelFormat pixelformat);
timer.Stop();
int t1 = timer.Duration;
timer.Start();
byte[] graybyte = BitmapHelper.Color2GrayByte(bmpbyte, width, height, stride, depth);
timer.Stop();
int t2 = timer.Duration;
timer.Start();
Bitmap graymap = BitmapHelper.ByteToBitmap(graybyte, width, height, pixelformat);
timer.Stop();
int t3 = timer.Duration;
RtbMsg.AppendText($"Bitmap转彩色一维字节数组,耗时{t1}毫秒,Padding={stride - width * depth / 8}{Environment.NewLine}");
RtbMsg.AppendText($"彩色一维数组转灰度一维数组,耗时{t2}毫秒{Environment.NewLine}");
RtbMsg.AppendText($"灰度一维数组转Bitmap,耗时{t3}毫秒{Environment.NewLine}{Environment.NewLine}");
RtbMsg.ScrollToCaret();
RtbMsg.AppendText($"{Environment.NewLine}");
Bitmap bmpclone = (Bitmap)bmp.Clone();
PbxSrc.Image = bmpclone;
PbxDest.Image = graymap;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。