c#位图。保存GDI+;中发生的一般错误;windows应用程序
我正在做OCR应用。我在运行系统时出现此错误,系统将把picturebox3.image保存到一个文件夹中c#位图。保存GDI+;中发生的一般错误;windows应用程序,c#,image,ocr,C#,Image,Ocr,我正在做OCR应用。我在运行系统时出现此错误,系统将把picturebox3.image保存到一个文件夹中 //When user is selecting, RegionSelect = true private bool RegionSelect = false; private int x0, x1, y0, y1; private Bitmap bmpImage; private void loadImageBT_Click(object sender, Eve
//When user is selecting, RegionSelect = true
private bool RegionSelect = false;
private int x0, x1, y0, y1;
private Bitmap bmpImage;
private void loadImageBT_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog open = new OpenFileDialog();
open.InitialDirectory = @"C:\Users\Shen\Desktop";
open.Filter = "Image Files(*.jpg; *.jpeg)|*.jpg; *.jpeg";
if (open.ShowDialog() == DialogResult.OK)
{
singleFileInfo = new FileInfo(open.FileName);
string dirName = System.IO.Path.GetDirectoryName(open.FileName);
loadTB.Text = open.FileName;
pictureBox1.Image = new Bitmap(open.FileName);
bmpImage = new Bitmap(pictureBox1.Image);
}
}
catch (Exception)
{
throw new ApplicationException("Failed loading image");
}
}
//User image selection Start Point
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
RegionSelect = true;
//Save the start point.
x0 = e.X;
y0 = e.Y;
}
//User select image progress
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
//Do nothing it we're not selecting an area.
if (!RegionSelect) return;
//Save the new point.
x1 = e.X;
y1 = e.Y;
//Make a Bitmap to display the selection rectangle.
Bitmap bm = new Bitmap(bmpImage);
//Draw the rectangle in the image.
using (Graphics g = Graphics.FromImage(bm))
{
g.DrawRectangle(Pens.Red, Math.Min(x0, x1), Math.Min(y0, y1), Math.Abs(x1 - x0), Math.Abs(y1 - y0));
}
//Temporary display the image.
pictureBox1.Image = bm;
}
//Image Selection End Point
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
// Do nothing it we're not selecting an area.
if (!RegionSelect) return;
RegionSelect = false;
//Display the original image.
pictureBox1.Image = bmpImage;
// Copy the selected part of the image.
int wid = Math.Abs(x0 - x1);
int hgt = Math.Abs(y0 - y1);
if ((wid < 1) || (hgt < 1)) return;
Bitmap area = new Bitmap(wid, hgt);
using (Graphics g = Graphics.FromImage(area))
{
Rectangle source_rectangle = new Rectangle(Math.Min(x0, x1), Math.Min(y0, y1), wid, hgt);
Rectangle dest_rectangle = new Rectangle(0, 0, wid, hgt);
g.DrawImage(bmpImage, dest_rectangle, source_rectangle, GraphicsUnit.Pixel);
}
// Display the result.
pictureBox3.Image = area;
** ERROR occuer here!!!!!**
area.Save(@"C:\Users\Shen\Desktop\LenzOCR\TempFolder\tempPic.jpg"); // error line occcur
singleFileInfo = new FileInfo("C:\\Users\\Shen\\Desktop\\LenzOCR\\TempFolder\\tempPic.jpg");
}
private void ScanBT_Click(object sender, EventArgs e)
{
var folder = @"C:\Users\Shen\Desktop\LenzOCR\LenzOCR\WindowsFormsApplication1\ImageFile";
DirectoryInfo directoryInfo;
FileInfo[] files;
directoryInfo = new DirectoryInfo(folder);
files = directoryInfo.GetFiles("*.jpg", SearchOption.AllDirectories);
var processImagesDelegate = new ProcessImagesDelegate(ProcessImages2);
processImagesDelegate.BeginInvoke(files, null, null);
//BackgroundWorker bw = new BackgroundWorker();
//bw.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
//bw.RunWorkerAsync(bw);
//bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
}
private void ProcessImages2(FileInfo[] files)
{
var comparableImages = new List<ComparableImage>();
var index = 0x0;
foreach (var file in files)
{
if (exit)
{
return;
}
var comparableImage = new ComparableImage(file);
comparableImages.Add(comparableImage);
index++;
}
index = 0;
similarityImagesSorted = new List<SimilarityImages>();
var fileImage = new ComparableImage(singleFileInfo);
for (var i = 0; i < comparableImages.Count; i++)
{
if (exit)
return;
var destination = comparableImages[i];
var similarity = fileImage.CalculateSimilarity(destination);
var sim = new SimilarityImages(fileImage, destination, similarity);
similarityImagesSorted.Add(sim);
index++;
}
similarityImagesSorted.Sort();
similarityImagesSorted.Reverse();
similarityImages = new BindingList<SimilarityImages>(similarityImagesSorted);
var buttons =
new List<Button>
{
ScanBT
};
if (similarityImages[0].Similarity > 70)
{
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "Data Source=SHEN-PC\\SQLEXPRESS;Initial Catalog=CharacterImage;Integrated Security=True";
con.Open();
String getFile = "SELECT ImageName, Character FROM CharacterImage WHERE ImageName='" + similarityImages[0].Destination.ToString() + "'";
SqlCommand cmd2 = new SqlCommand(getFile, con);
SqlDataReader rd2 = cmd2.ExecuteReader();
while (rd2.Read())
{
for (int i = 0; i < 1; i++)
{
string getText = rd2["Character"].ToString();
Action showText = () => ocrTB.AppendText(getText);
ocrTB.Invoke(showText);
}
}
con.Close();
}
else
{
MessageBox.Show("No character found!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
//当用户选择时,RegionSelect=true
私有布尔区域选择=假;
私有整数x0,x1,y0,y1;
私家车;
私有void loadImageBT_单击(对象发送方,事件参数e)
{
尝试
{
OpenFileDialog open=新建OpenFileDialog();
open.InitialDirectory=@“C:\Users\Shen\Desktop”;
打开.Filter=“图像文件(*.jpg;*.jpeg)|*.jpg;*.jpeg”;
if(open.ShowDialog()==DialogResult.OK)
{
singleFileInfo=新文件信息(open.FileName);
字符串dirName=System.IO.Path.GetDirectoryName(open.FileName);
loadTB.Text=open.FileName;
pictureBox1.Image=新位图(打开.FileName);
bmpImage=新位图(pictureBox1.Image);
}
}
捕获(例外)
{
抛出新的ApplicationException(“加载映像失败”);
}
}
//用户图像选择起点
私有void pictureBox1\u MouseDown(对象发送方,MouseEventArgs e)
{
RegionSelect=true;
//保存起点。
x0=e.X;
y0=e.Y;
}
//用户选择图像进度
私有void pictureBox1\u MouseMove(对象发送方,MouseEventArgs e)
{
//什么都不要做,我们没有选择一个区域。
如果(!RegionSelect)返回;
//保存新点。
x1=e.X;
y1=e.Y;
//制作位图以显示选择矩形。
位图bm=新位图(bmp图像);
//在图像中绘制矩形。
使用(Graphics g=Graphics.FromImage(bm))
{
g、 绘制矩形(Pens.Red,Math.Min(x0,x1),Math.Min(y0,y1),Math.Abs(x1-x0),Math.Abs(y1-y0));
}
//临时显示图像。
pictureBox1.Image=bm;
}
//图像选择终点
私有无效图片box1u MouseUp(对象发送器,MouseEventArgs e)
{
//什么都不要做,我们没有选择一个区域。
如果(!RegionSelect)返回;
RegionSelect=false;
//显示原始图像。
pictureBox1.Image=bmp图像;
//复制图像的选定部分。
int-wid=Math.Abs(x0-x1);
int hgt=数学绝对值(y0-y1);
如果((wid<1)|(hgt<1))返回;
位图区域=新位图(wid、hgt);
使用(Graphics g=Graphics.FromImage(area))
{
矩形源_矩形=新矩形(数学最小值(x0,x1),数学最小值(y0,y1),wid,hgt);
矩形dest_Rectangle=新矩形(0,0,wid,hgt);
g、 DrawImage(bmpImage、dest_矩形、source_矩形、GraphicsUnit.Pixel);
}
//显示结果。
pictureBox3.图像=面积;
**这里发生错误**
area.Save(@“C:\Users\Shen\Desktop\LenzOCR\TempFolder\tempPic.jpg”);//错误行occcur
singleFileInfo=newFileInfo(“C:\\Users\\Shen\\Desktop\\LenzOCR\\TempFolder\\tempPic.jpg”);
}
私有void ScanBT_单击(对象发送方,事件参数e)
{
var folder=@“C:\Users\Shen\Desktop\LenzOCR\LenzOCR\WindowsFormsApplication1\ImageFile”;
目录信息目录信息;
FileInfo[]文件;
directoryInfo=新的directoryInfo(文件夹);
files=directoryInfo.GetFiles(“*.jpg”,SearchOption.AllDirectories);
var processImagesDelegate=新的processImagesDelegate(ProcessImages2);
processImagesDelegate.BeginInvoke(文件,null,null);
//BackgroundWorker bw=新的BackgroundWorker();
//bw.DoWork+=新DoWorkEventHandler(后台工作1_DoWork);
//RunWorkerAsync(bw);
//bw.RunWorkerCompleted+=新的RunWorkerCompletedEventHandler(backgroundWorker1\u RunWorkerCompleted);
}
私有void ProcessImages2(FileInfo[]文件)
{
var comparableImages=新列表();
var指数=0x0;
foreach(文件中的var文件)
{
如果(退出)
{
返回;
}
var comparableImage=新的comparableImage(文件);
可比图像。添加(可比图像);
索引++;
}
指数=0;
SimilarityImagesOrted=新列表();
var fileImage=新的可比较图像(singleFileInfo);
对于(var i=0;i70)
{
con=new System.Data.SqlClient.SqlConnection();
con.ConnectionString=“数据源=SHEN-PC\\SQLEXPRESS;初始目录=CharacterImage;集成安全性=True”;
con.Open();
String getFile=“选择ImageName,CharacterImage中的字符,其中ImageName=”+similarityImages[0]。Destination.ToString()+”;
SqlCommand cmd2=新的SqlCommand(getFile,con);
SqlDataReader rd2=cmd2.ExecuteReader();
while(rd2.Read())
{
对于(int i=0;i<1;i++)
{
字符串getText=rd2[“Character”].ToString();
动作showText=()=>ocrTB.AppendText(getText);
ocrTB.Invoke(showText);
}
}
con.Close();
}
其他的
{
Show(“未找到字符!”、“错误!”、MessageBoxButtons.OK、MessageBoxIcon.Error);
}
}
#端区
已经有一段时间了,我希望您能找到答案,但我猜您在保存jpeg时需要设置文件格式:
area.Save(@"C:\Users\Shen\Desktop\LenzOCR\TempFolder\tempPic.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
除此之外,我不记得picturebox控件是否是双缓冲的,这可能是问题所在(如果不是,您可能无法在渲染时访问它以进行保存,但是如果
Bitmap SavingObject=new Bitmap(area);
picturebox3.Image=area;
SavingObject.Save(@"C:\Users\Shen\Desktop\LenzOCR\TempFolder\tempPic.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);