EDN Admin
Well-known member
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using DannyGeneral;
<span style="color:Blue; using System.Drawing.Drawing2D;
<span style="color:Blue; namespace Batch_Images_Convertion
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
<span style="color:Blue; bool cancel_;
<span style="color:Blue; bool automatic_;
<span style="color:Blue; public Form1()
{
InitializeComponent();
automatic_ = <span style="color:Blue; false;
checkBox1.Checked = Options_DB.Get_Automatic_Start();
<span style="color:Blue; if (checkBox1.Checked == <span style="color:Blue; true)
{
backgroundWorker1.DoWork += <span style="color:Blue; new DoWorkEventHandler(backgroundWorker1_DoWork_1);
automatic_ = <span style="color:Blue; true;
}
cancel_ = <span style="color:Blue; false;
backgroundWorker1.RunWorkerAsync();
}
<span style="color:Blue; private <span style="color:Blue; void beginConvertionToolStripMenuItem_Click(<span style="color:Blue; object sender, EventArgs e)
{
backgroundWorker1.DoWork += <span style="color:Blue; new DoWorkEventHandler(backgroundWorker1_DoWork_1);
automatic_ = <span style="color:Blue; true;
button4.Enabled = <span style="color:Blue; true;
beginConvertionToolStripMenuItem.Enabled = <span style="color:Blue; false;
backgroundWorker1.RunWorkerAsync();
}
<span style="color:Blue; private <span style="color:Blue; void aboutToolStripMenuItem_Click(<span style="color:Blue; object sender, EventArgs e)
{
}
<span style="color:Blue; private <span style="color:Blue; void exitToolStripMenuItem_Click(<span style="color:Blue; object sender, EventArgs e)
{
}
<span style="color:Blue; private <span style="color:Blue; void button2_Click(<span style="color:Blue; object sender, EventArgs e)
{
}
<span style="color:Blue; private <span style="color:Blue; void listView1_ItemDrag(<span style="color:Blue; object sender, ItemDragEventArgs e)
{
}
<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{
}
<span style="color:Blue; private <span style="color:Blue; void backgroundWorker1_ProgressChanged(<span style="color:Blue; object sender, ProgressChangedEventArgs e)
{
listBox1.Items.Add( <span style="color:#A31515; "Converting File: " + e.UserState.ToString());
<span style="color:Green; //progressBar1.Value = e.ProgressPercentage;
<span style="color:Green; //this.Text = e.ProgressPercentage.ToString();
}
<span style="color:Blue; private <span style="color:Blue; void backgroundWorker1_RunWorkerCompleted(<span style="color:Blue; object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show(<span style="color:#A31515; "comleted");
<span style="color:Blue; if (e.Error != <span style="color:Blue; null)
{
MessageBox.Show(e.Error.Message);
}
<span style="color:Blue; else <span style="color:Blue; if (e.Cancelled)
{
listBox1.Items.Add(<span style="color:#A31515; "Convertion Have been canceled");
button4.Enabled = <span style="color:Blue; false;
}
<span style="color:Blue; if (cancel_ == <span style="color:Blue; true)
{
}
<span style="color:Blue; else
{
<span style="color:Green; // listBox1.Items.Add(e.Result.ToString());
button4.Enabled = <span style="color:Blue; false;
}
}
<span style="color:Blue; private <span style="color:Blue; void button4_Click(<span style="color:Blue; object sender, EventArgs e)
{
cancel_ = <span style="color:Blue; true;
beginConvertionToolStripMenuItem.Enabled = <span style="color:Blue; true;
}
<span style="color:Blue; private <span style="color:Blue; void listBox1_DragDrop(<span style="color:Blue; object sender, DragEventArgs e)
{
<span style="color:Blue; if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
<span style="color:Blue; string[] files = (<span style="color:Blue; string[])e.Data.GetData(DataFormats.FileDrop);
<span style="color:Gray; ///<span style="color:Green; /either
<span style="color:Green; //for (int i = 0; i < files.Length; i++)
<span style="color:Green; //{
<span style="color:Green; // FileInfo fi = new FileInfo(files);
<span style="color:Green; // //add more extensions here
<span style="color:Green; // if (fi.Extension == ".jpg" || fi.Extension == ".png")
<span style="color:Green; // {
<span style="color:Green; // //do something with the files
<span style="color:Green; // }
<span style="color:Green; //}
<span style="color:Green; //or maybe just
FileInfo fi = <span style="color:Blue; new FileInfo(files[0]);
<span style="color:Blue; if (listBox1.Items.Contains(fi.FullName))
{
}
<span style="color:Blue; else
{
listBox1.Items.Add(fi.FullName);
}
}
}
<span style="color:Blue; private <span style="color:Blue; void listBox1_DragOver(<span style="color:Blue; object sender, DragEventArgs e)
{
<span style="color:Blue; if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
<span style="color:Blue; string[] files = (<span style="color:Blue; string[])e.Data.GetData(DataFormats.FileDrop);
<span style="color:Blue; bool bfound = <span style="color:Blue; false;
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < files.Length; i++)
{
FileInfo fi = <span style="color:Blue; new FileInfo(files);
<span style="color:Green; //add more extensions here
<span style="color:Blue; if (fi.Extension == <span style="color:#A31515; ".jpg" || fi.Extension == <span style="color:#A31515; ".png" || fi.Extension == <span style="color:#A31515; ".bmp" || fi.Extension == <span style="color:#A31515; ".emf" || fi.Extension == <span style="color:#A31515; ".gif" || fi.Extension == <span style="color:#A31515; ".ico" || fi.Extension == <span style="color:#A31515; ".tiff"
|| fi.Extension == <span style="color:#A31515; ".wmf" || fi.Extension == <span style="color:#A31515; ".exif")
{
bfound = <span style="color:Blue; true;
<span style="color:Blue; break;
}
}
<span style="color:Blue; if (bfound)
e.Effect = DragDropEffects.Copy;
<span style="color:Blue; else
e.Effect = DragDropEffects.None;
}
<span style="color:Blue; else
e.Effect = DragDropEffects.None;
}
<span style="color:Blue; private <span style="color:Blue; void checkBox1_CheckedChanged(<span style="color:Blue; object sender, EventArgs e)
{
Options_DB.set_automatic_start(checkBox1.Checked);
}
<span style="color:Blue; private <span style="color:Blue; void backgroundWorker1_DoWork_1(<span style="color:Blue; object sender, DoWorkEventArgs e)
{
<span style="color:Blue; if (automatic_ == <span style="color:Blue; true)
{
<span style="color:Blue; int y;
<span style="color:Blue; string[] newDest_files;
<span style="color:Green; /* newDest_files = Directory.GetFiles(@"D:New folder (19)","*.*");//@"d:test", "*.*");
for (y = 0; y < newDest_files.Length; y++)
{
File.Delete(newDest_files[y]);
}*/
System.Drawing.Image NewImages;
<span style="color:Blue; string sf;
<span style="color:Blue; string[] images;
<span style="color:Blue; int x;
sf = <span style="color:#A31515; @"D:New folder (19)";<span style="color:Green; //@"d:RadarImagesDownloaded";
images = Directory.GetFiles(sf, <span style="color:#A31515; "*.jpg");
<span style="color:Blue; for (x = 0; x < images.Length; x++)
{
backgroundWorker1.ReportProgress(x, images[x]);
<span style="color:Blue; if (cancel_ == <span style="color:Blue; true)
{
e.Cancel = <span style="color:Blue; true;
<span style="color:Blue; break;
}
<span style="color:Blue; if (File.Exists(images[x]))
{
NewImages = System.Drawing.Image.FromFile(images[x]);
<span style="color:Green; //NewImages.Save(@"d:testConvertedImage" + x.ToString("D1") /*x.ToString("D6")* */+ ".Bmp", System.Drawing.Imaging.ImageFormat.Bmp);
<span style="color:Green; /* if (images[x] == @"d:RadarImagesDownloadedradar000100.jpg") //x == 89)
{
break;
}*/
batch_Resize(NewImages);
}
}
}
e.Result = <span style="color:#A31515; "All files converted successfully";
}
<span style="color:Blue; private Image batch_Resize(Image sourceImage)
{
Bitmap newImage = <span style="color:Blue; new Bitmap(sourceImage.Width, sourceImage.Height);
<span style="color:Blue; using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(newImage, <span style="color:Blue; new Rectangle(0, 0, sourceImage.Width, sourceImage.Height));
newImage.Save(<span style="color:#A31515; @"d:New folder (20)" + <span style="color:#A31515; "ggg.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
<span style="color:Blue; return newImage;
}
}
}
}
[/code]
<br/>
The error out of memory is on the line: gr.DrawImage(newImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height));
And lets say in this case that each image file resolution is: Width 5184 and Height 3456 and i want the output to be resolution of 512x512
The out of memory full exception error message:
System.OutOfMemoryException was unhandled by user code<br/>
Message=Out of memory.<br/>
Source=System.Drawing<br/>
StackTrace:<br/>
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)<br/>
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)<br/>
at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)<br/>
at Batch_Images_Convertion.Form1.batch_Resize(Image sourceImage) in D:C-SharpBatch_Images_ConvertionBatch_Images_ConvertionBatch_Images_ConvertionForm1.cs:line 247<br/>
at Batch_Images_Convertion.Form1.backgroundWorker1_DoWork_1(Object sender, DoWorkEventArgs e) in D:C-SharpBatch_Images_ConvertionBatch_Images_ConvertionBatch_Images_ConvertionForm1.cs:line 229<br/>
at System.ComponentModel.DoWorkEventHandler.Invoke(Object sender, DoWorkEventArgs e)<br/>
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)<br/>
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)<br/>
InnerException:
Thanks. <hr class="sig danieli
View the full article
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using DannyGeneral;
<span style="color:Blue; using System.Drawing.Drawing2D;
<span style="color:Blue; namespace Batch_Images_Convertion
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
<span style="color:Blue; bool cancel_;
<span style="color:Blue; bool automatic_;
<span style="color:Blue; public Form1()
{
InitializeComponent();
automatic_ = <span style="color:Blue; false;
checkBox1.Checked = Options_DB.Get_Automatic_Start();
<span style="color:Blue; if (checkBox1.Checked == <span style="color:Blue; true)
{
backgroundWorker1.DoWork += <span style="color:Blue; new DoWorkEventHandler(backgroundWorker1_DoWork_1);
automatic_ = <span style="color:Blue; true;
}
cancel_ = <span style="color:Blue; false;
backgroundWorker1.RunWorkerAsync();
}
<span style="color:Blue; private <span style="color:Blue; void beginConvertionToolStripMenuItem_Click(<span style="color:Blue; object sender, EventArgs e)
{
backgroundWorker1.DoWork += <span style="color:Blue; new DoWorkEventHandler(backgroundWorker1_DoWork_1);
automatic_ = <span style="color:Blue; true;
button4.Enabled = <span style="color:Blue; true;
beginConvertionToolStripMenuItem.Enabled = <span style="color:Blue; false;
backgroundWorker1.RunWorkerAsync();
}
<span style="color:Blue; private <span style="color:Blue; void aboutToolStripMenuItem_Click(<span style="color:Blue; object sender, EventArgs e)
{
}
<span style="color:Blue; private <span style="color:Blue; void exitToolStripMenuItem_Click(<span style="color:Blue; object sender, EventArgs e)
{
}
<span style="color:Blue; private <span style="color:Blue; void button2_Click(<span style="color:Blue; object sender, EventArgs e)
{
}
<span style="color:Blue; private <span style="color:Blue; void listView1_ItemDrag(<span style="color:Blue; object sender, ItemDragEventArgs e)
{
}
<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{
}
<span style="color:Blue; private <span style="color:Blue; void backgroundWorker1_ProgressChanged(<span style="color:Blue; object sender, ProgressChangedEventArgs e)
{
listBox1.Items.Add( <span style="color:#A31515; "Converting File: " + e.UserState.ToString());
<span style="color:Green; //progressBar1.Value = e.ProgressPercentage;
<span style="color:Green; //this.Text = e.ProgressPercentage.ToString();
}
<span style="color:Blue; private <span style="color:Blue; void backgroundWorker1_RunWorkerCompleted(<span style="color:Blue; object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show(<span style="color:#A31515; "comleted");
<span style="color:Blue; if (e.Error != <span style="color:Blue; null)
{
MessageBox.Show(e.Error.Message);
}
<span style="color:Blue; else <span style="color:Blue; if (e.Cancelled)
{
listBox1.Items.Add(<span style="color:#A31515; "Convertion Have been canceled");
button4.Enabled = <span style="color:Blue; false;
}
<span style="color:Blue; if (cancel_ == <span style="color:Blue; true)
{
}
<span style="color:Blue; else
{
<span style="color:Green; // listBox1.Items.Add(e.Result.ToString());
button4.Enabled = <span style="color:Blue; false;
}
}
<span style="color:Blue; private <span style="color:Blue; void button4_Click(<span style="color:Blue; object sender, EventArgs e)
{
cancel_ = <span style="color:Blue; true;
beginConvertionToolStripMenuItem.Enabled = <span style="color:Blue; true;
}
<span style="color:Blue; private <span style="color:Blue; void listBox1_DragDrop(<span style="color:Blue; object sender, DragEventArgs e)
{
<span style="color:Blue; if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
<span style="color:Blue; string[] files = (<span style="color:Blue; string[])e.Data.GetData(DataFormats.FileDrop);
<span style="color:Gray; ///<span style="color:Green; /either
<span style="color:Green; //for (int i = 0; i < files.Length; i++)
<span style="color:Green; //{
<span style="color:Green; // FileInfo fi = new FileInfo(files);
<span style="color:Green; // //add more extensions here
<span style="color:Green; // if (fi.Extension == ".jpg" || fi.Extension == ".png")
<span style="color:Green; // {
<span style="color:Green; // //do something with the files
<span style="color:Green; // }
<span style="color:Green; //}
<span style="color:Green; //or maybe just
FileInfo fi = <span style="color:Blue; new FileInfo(files[0]);
<span style="color:Blue; if (listBox1.Items.Contains(fi.FullName))
{
}
<span style="color:Blue; else
{
listBox1.Items.Add(fi.FullName);
}
}
}
<span style="color:Blue; private <span style="color:Blue; void listBox1_DragOver(<span style="color:Blue; object sender, DragEventArgs e)
{
<span style="color:Blue; if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
<span style="color:Blue; string[] files = (<span style="color:Blue; string[])e.Data.GetData(DataFormats.FileDrop);
<span style="color:Blue; bool bfound = <span style="color:Blue; false;
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < files.Length; i++)
{
FileInfo fi = <span style="color:Blue; new FileInfo(files);
<span style="color:Green; //add more extensions here
<span style="color:Blue; if (fi.Extension == <span style="color:#A31515; ".jpg" || fi.Extension == <span style="color:#A31515; ".png" || fi.Extension == <span style="color:#A31515; ".bmp" || fi.Extension == <span style="color:#A31515; ".emf" || fi.Extension == <span style="color:#A31515; ".gif" || fi.Extension == <span style="color:#A31515; ".ico" || fi.Extension == <span style="color:#A31515; ".tiff"
|| fi.Extension == <span style="color:#A31515; ".wmf" || fi.Extension == <span style="color:#A31515; ".exif")
{
bfound = <span style="color:Blue; true;
<span style="color:Blue; break;
}
}
<span style="color:Blue; if (bfound)
e.Effect = DragDropEffects.Copy;
<span style="color:Blue; else
e.Effect = DragDropEffects.None;
}
<span style="color:Blue; else
e.Effect = DragDropEffects.None;
}
<span style="color:Blue; private <span style="color:Blue; void checkBox1_CheckedChanged(<span style="color:Blue; object sender, EventArgs e)
{
Options_DB.set_automatic_start(checkBox1.Checked);
}
<span style="color:Blue; private <span style="color:Blue; void backgroundWorker1_DoWork_1(<span style="color:Blue; object sender, DoWorkEventArgs e)
{
<span style="color:Blue; if (automatic_ == <span style="color:Blue; true)
{
<span style="color:Blue; int y;
<span style="color:Blue; string[] newDest_files;
<span style="color:Green; /* newDest_files = Directory.GetFiles(@"D:New folder (19)","*.*");//@"d:test", "*.*");
for (y = 0; y < newDest_files.Length; y++)
{
File.Delete(newDest_files[y]);
}*/
System.Drawing.Image NewImages;
<span style="color:Blue; string sf;
<span style="color:Blue; string[] images;
<span style="color:Blue; int x;
sf = <span style="color:#A31515; @"D:New folder (19)";<span style="color:Green; //@"d:RadarImagesDownloaded";
images = Directory.GetFiles(sf, <span style="color:#A31515; "*.jpg");
<span style="color:Blue; for (x = 0; x < images.Length; x++)
{
backgroundWorker1.ReportProgress(x, images[x]);
<span style="color:Blue; if (cancel_ == <span style="color:Blue; true)
{
e.Cancel = <span style="color:Blue; true;
<span style="color:Blue; break;
}
<span style="color:Blue; if (File.Exists(images[x]))
{
NewImages = System.Drawing.Image.FromFile(images[x]);
<span style="color:Green; //NewImages.Save(@"d:testConvertedImage" + x.ToString("D1") /*x.ToString("D6")* */+ ".Bmp", System.Drawing.Imaging.ImageFormat.Bmp);
<span style="color:Green; /* if (images[x] == @"d:RadarImagesDownloadedradar000100.jpg") //x == 89)
{
break;
}*/
batch_Resize(NewImages);
}
}
}
e.Result = <span style="color:#A31515; "All files converted successfully";
}
<span style="color:Blue; private Image batch_Resize(Image sourceImage)
{
Bitmap newImage = <span style="color:Blue; new Bitmap(sourceImage.Width, sourceImage.Height);
<span style="color:Blue; using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(newImage, <span style="color:Blue; new Rectangle(0, 0, sourceImage.Width, sourceImage.Height));
newImage.Save(<span style="color:#A31515; @"d:New folder (20)" + <span style="color:#A31515; "ggg.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
<span style="color:Blue; return newImage;
}
}
}
}
[/code]
<br/>
The error out of memory is on the line: gr.DrawImage(newImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height));
And lets say in this case that each image file resolution is: Width 5184 and Height 3456 and i want the output to be resolution of 512x512
The out of memory full exception error message:
System.OutOfMemoryException was unhandled by user code<br/>
Message=Out of memory.<br/>
Source=System.Drawing<br/>
StackTrace:<br/>
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)<br/>
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)<br/>
at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)<br/>
at Batch_Images_Convertion.Form1.batch_Resize(Image sourceImage) in D:C-SharpBatch_Images_ConvertionBatch_Images_ConvertionBatch_Images_ConvertionForm1.cs:line 247<br/>
at Batch_Images_Convertion.Form1.backgroundWorker1_DoWork_1(Object sender, DoWorkEventArgs e) in D:C-SharpBatch_Images_ConvertionBatch_Images_ConvertionBatch_Images_ConvertionForm1.cs:line 229<br/>
at System.ComponentModel.DoWorkEventHandler.Invoke(Object sender, DoWorkEventArgs e)<br/>
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)<br/>
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)<br/>
InnerException:
Thanks. <hr class="sig danieli
View the full article