C# 在c中插入图像、灰度和擦除过渡#
我已经用c语言开发了一个windows应用程序# 此应用程序有三个按钮(打开、保存、灰度) 并具有图片框,用于在应用程序中显示图片C# 在c中插入图像、灰度和擦除过渡#,c#,windows,C#,Windows,我已经用c语言开发了一个windows应用程序# 此应用程序有三个按钮(打开、保存、灰度) 并具有图片框,用于在应用程序中显示图片 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace MyFirstWindowsApplication
{
public partial class Form1 : Form
{
Bitmap newbitmap;
Bitmap newbitmap2;
Bitmap outp;
Image file;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
file = Image.FromFile(openFileDialog1.FileName);
newbitmap = new Bitmap(openFileDialog1.FileName);
newbitmap2 = new Bitmap(openFileDialog1.FileName);
outp = new Bitmap(openFileDialog1.FileName);
pictureBox1.Image = file;
}
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
if (newbitmap != null)
{
if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "bmp")
{
newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "jpg")
{
newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "bmp")
{
newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);
}
if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 4).ToLower() == "jpeg")
{
newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);
}
if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "png")
{
newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Png);
}
if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "gif")
{
newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Gif);
}
}
else
{
MessageBox.Show("you need to open file first");
}
}
}
private void button3_Click(object sender, EventArgs e)
{
for (int x = 0; x < newbitmap.Width; x++)
{
for (int y = 0; y < newbitmap.Height; y++)
{
Color originalColor = newbitmap.GetPixel(x, y);
int grayscale = (int)((originalColor.R * .3) + (originalColor.G * .59) + (originalColor.B * .11));
Color newColor = Color.FromArgb(grayscale, grayscale, grayscale);
newbitmap.SetPixel(x, y, newColor);
}
}
int tmax = 10;
int xmax=newbitmap.Width;
int ymax=newbitmap.Height;
for (int t = 0; t <= tmax; t += 1)
{
for (int x = 0; x < xmax; x++)
{
for (int y = 0; y < ymax; y++)
{
if ((x / xmax) > (t / tmax))
{
Color originalco = newbitmap2.GetPixel(x, y);
outp.SetPixel(x, y, originalco);
}
else
{
Color originalco3 = newbitmap.GetPixel(x, y); ;
outp.SetPixel(x, y, originalco3);
}
pictureBox1.Image = outp;
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用系统、绘图、成像;
命名空间MyFirstWindows应用程序
{
公共部分类Form1:Form
{
位图新位图;
位图newbitmap2;
位图输出;
图像文件;
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
DialogResult dr=openFileDialog1.ShowDialog();
if(dr==DialogResult.OK)
{
file=Image.FromFile(openFileDialog1.FileName);
newbitmap=新位图(openFileDialog1.FileName);
newbitmap2=新位图(openFileDialog1.FileName);
outp=新位图(openFileDialog1.FileName);
pictureBox1.Image=文件;
}
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
DialogResult dr=saveFileDialog1.ShowDialog();
if(dr==DialogResult.OK)
{
if(newbitmap!=null)
{
if(saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length-3).ToLower()=“bmp”)
{
保存(saveFileDialog1.FileName,ImageFormat.Bmp);
}
if(saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length-3).ToLower()=“jpg”)
{
保存(saveFileDialog1.FileName,ImageFormat.Bmp);
}
if(saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length-3).ToLower()=“bmp”)
{
保存(saveFileDialog1.FileName,ImageFormat.Jpeg);
}
if(saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length-4).ToLower()=“jpeg”)
{
保存(saveFileDialog1.FileName,ImageFormat.Jpeg);
}
if(saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length-3).ToLower()=“png”)
{
保存(saveFileDialog1.FileName,ImageFormat.Png);
}
if(saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length-3).ToLower()=“gif”)
{
保存(saveFileDialog1.FileName,ImageFormat.Gif);
}
}
其他的
{
Show(“您需要先打开文件”);
}
}
}
私有无效按钮3\u单击(对象发送者,事件参数e)
{
对于(int x=0;x
问题是不做擦拭过渡< P>使灰度图像考虑使用这个更好的解决方案
public Image MakeGrayscale(Image original)
{
Image newBitmap = new Bitmap(original.Width, original.Height);
Graphics g = Graphics.FromImage(newBitmap);
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {0.299f, 0.299f, 0.299f, 0, 0},
new float[] {0.587f, 0.587f, 0.587f, 0, 0},
new float[] {.114f, .114f, .114f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
g.Dispose();
return newBitmap;
}
然后
pictureBox1.Image = MakeGrayscale(newbitmap);
即使在保存例程中也会出现一些错误。试试这个:
private void button2_Click(object sender, EventArgs e)
{
if (newbitmap == null)
{
MessageBox.Show("you need to open file first");
return;
}
if (DialogResult dr = saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string ext = Path.GetExtension(saveFileDialog1.FileName).ToLower();
switch (ext)
{
case ".bmp":
newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
break;
case ".jpg":
case ".jpeg":
newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);
break;
case ".png":
newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Png);
break;
case ".gif":
newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Gif);
break;
default: MessageBox.Show("Extension not supported");
}
}
}
在我看来,转换之所以不起作用是因为这个原因:您的主线程(需要理解的GUI)在循环中快速(非常快)更改picturebox图像,但在这个循环中,它没有时间更新GUI,因此picturebox只在退出循环时有效地更改。。所以你看不到转变。您应该使用BackgroundWorker更改picturebox图像(在每个循环之间暂停,以便人眼看到新图像).不,我不是指灰度,因为它与我的代码一起工作,我是指第三个for循环,它包含一个擦除转换,应该工作,但我不知道是不是问题。不,伙计,所有代码都与我一起工作,我只想为第三个使用正确的语法loop@OmarAl-哈马维:读我编辑过的答案。无论如何,你的代码是错误的,所以看看它。我的灰度代码更有效。您的保存例程有两次bmp,并且没有使用正确的路径函数来获取文件扩展名。:)@OmarAl hamawi:最后,考虑接受一些人给你的答案吧!奥马尔·哈马维:看