Emgu body measuring - c#

I'm developing a uniform fitting application, the program will take a picture of you and from that picture it will give you your polo/blouse size (Small, Medium, Large, X-Large)
My problem is how will I know if the user using the program has the size of Small? Medium? or Large?
here's my code:
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 Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
namespace fitting
{
public partial class Form1 : Form
{
HaarCascade UpperBody = new HaarCascade("haarcascade_mcs_upperbody.xml");
HaarCascade LowerBody = new HaarCascade("haarcascade_lowerbody.xml");
Capture camera;
bool captureProcess = false;
Image<Bgr, Byte> img;
public Form1()
{
InitializeComponent();
}
void viewImage(object sender, EventArgs e)
{
img = camera.QueryFrame();
if (img == null)
return;
CamImageBox.Image = img;
}
private void btnCapture_Click(object sender, EventArgs e)
{
if (captureProcess == true)
{
string data;
Application.Idle -= viewImage;
captureProcess = false;
SaveFileDialog dlg = new SaveFileDialog();
//dlg="Image|*.jpg;*png";
if (dlg.ShowDialog() == DialogResult.OK)
{
img.ToBitmap().Save(dlg.FileName + ".jpg", System.Drawing.Imaging.ImageFormat.Png);
data = dlg.FileName + ".jpg";
}
measureImage();
}
}
void measureImage()
{
OpenFileDialog dlg2 = new OpenFileDialog();
dlg2.Filter = "Image|*.jpg;*png";
if (dlg2.ShowDialog() == DialogResult.OK)
{
Image<Bgr, Byte> frame = new Image<Bgr, byte>(dlg2.FileName);
Image<Gray, Byte> Gray_Frame = frame.Convert<Gray, Byte>();
//1.985603925968
MCvAvgComp[][] LowerBodyDetect = Gray_Frame.DetectHaarCascade(
LowerBody,
1.985603925968,
0,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size());
MCvAvgComp[][] UpperBodyDetect = Gray_Frame.DetectHaarCascade(
UpperBody,
1.3,
5,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size());
//foreach (MCvAvgComp Upp_Body in UpperBodyDetect[0])
//{
// frame.Draw(Upp_Body.rect, new Bgr(Color.Red), 2);
// double width = (Upp_Body.rect.Width * 0.264583333);
// textBox1.Text = (Convert.ToString(width));
//}
try
{
frame.Draw(UpperBodyDetect[0][0].rect, new Bgr(Color.Red), 2);
double width = (UpperBodyDetect[0][0].rect.Width);
textBox1.Text = (Convert.ToString(width));
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
//foreach (MCvAvgComp Low_Body in LowerBodyDetect[0])
//{
// frame.Draw(Low_Body.rect, new Bgr(Color.Green), 2);
//}
try
{
frame.Draw(LowerBodyDetect[0][0].rect, new Bgr(Color.Green), 2);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
CamImageBox.Image = frame;
}
}
private void Form1_Load(object sender, EventArgs e)
{
bool useCam = false;
if (!useCam)
measureImage();
else {
try
{
camera = new Capture();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
return;
}
Application.Idle += viewImage;
captureProcess = true;
}
}
}
}

Your first problem is that your users can have various angle of views with different webcams.
Second problem is that you have to estimate the distance from user to his webcam because the more distant he is the smaller will be any dimension you measure.
I think you need to measure waist diameter etc... Is it ok or moral to ask user to take 2 photos? One directly facing the camera and one from profile. From this you could estimate the values...
So search for camera distance estimation algorithm.
After you have all sizes measured you can select proper size (small, medium...) from a table, see for example this

Related

C# RDLC Report Printing [duplicate]

I have an application where I have to print a RDLC report without showing the printDialog and using the default specified printer defined in the application. Below is my test implementaion code.
Microsoft.Reporting.WinForms.ReportViewer reportViewerSales = new Microsoft.Reporting.WinForms.ReportViewer();
Microsoft.Reporting.WinForms.ReportDataSource reportDataSourceSales = new Microsoft.Reporting.WinForms.ReportDataSource();
reportViewerSales.Reset();
reportViewerSales.LocalReport.ReportPath = #"Sales.rdlc";
reportDataSourceSales.Name = "SalesTableDataSet";
int i = 1;
foreach (Product item in ProductSalesList)
{
dataset.CurrentSales.AddCurrentSalesRow(i, item.Name, item.Quantity.ToString(), item.Price.ToString(), item.Price.ToString());
i++;
}
reportDataSourceSales.Value = dataset.CurrentSales;
reportViewerSales.LocalReport.DataSources.Add(reportDataSourceSales);
dataset.EndInit();
reportViewerSales.RefreshReport();
reportViewerSales.RenderingComplete += new RenderingCompleteEventHandler(PrintSales);
And here is my Rendering Complete Method
public void PrintSales(object sender, RenderingCompleteEventArgs e)
{
try
{
reportViewerSales.PrintDialog();
reportViewerSales.Clear();
reportViewerSales.LocalReport.ReleaseSandboxAppDomain();
}
catch (Exception ex)
{
}
}
I just gave a quick look to a class I created to print directly and I think I took some ideas from this walkthrough:
Printing a Local Report without Preview
i have made an extension class to #tezzos answer. which might make it more easier.
use this Gist Here to get the extension class i wrote. include it to your project. don't for get namespace :D
LocalReport report = new LocalReport();
report.ReportEmbeddedResource = "Your.Reports.Path.rdlc";
report.DataSources.Add(new ReportDataSource("DataSet1", getYourDatasource()));
report.PrintToPrinter();
PrintToPrinter Method will be available on LocalReport. Might Help someone
Download C# full Project file
using Microsoft.Reporting.WinForms;
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 WindowsFormsApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.UiLabelUpTableAdapter.Fill(this.POSDataSet.UiLabelUp);
this.reportViewer1.RefreshReport();
}
private void buttonPrintReport_Click(object sender, EventArgs e)
{
ReportViewer InvoiceReport = new ReportViewer();
InvoiceReport.LocalReport.ReportPath = #"C:\Users\Chamod\source\repos\WindowsFormsApp\WindowsFormsApp\Report1.rdlc";
ReportDataSource DataSet1 = new ReportDataSource("DataSet1", UiLabelUpBindingSource);
InvoiceReport.LocalReport.DataSources.Add(DataSet1);
LocalReport lr = InvoiceReport.LocalReport;
lr.PrintToPrinter();
InvoiceReport.Clear();
}
}
}
using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
namespace WindowsFormsApp
{
public static class LocalReportExtensions
{
public static void PrintToPrinter(this LocalReport report)
{
PageSettings pageSettings = new PageSettings();
pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
pageSettings.Margins = report.GetDefaultPageSettings().Margins;
Print(report, pageSettings);
}
public static void Print(this LocalReport report, PageSettings pageSettings)
{
string deviceInfo =
$#"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
<PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
<MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
<MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
<MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
<MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
</DeviceInfo>";
Warning[] warnings;
var streams = new List<Stream>();
var pageIndex = 0;
report.Render("Image", deviceInfo,
(name, fileNameExtension, encoding, mimeType, willSeek) =>
{
MemoryStream stream = new MemoryStream();
streams.Add(stream);
return stream;
}, out warnings);
foreach (Stream stream in streams)
stream.Position = 0;
if (streams == null || streams.Count == 0)
throw new Exception("No stream to print.");
using (PrintDocument printDocument = new PrintDocument())
{
printDocument.DefaultPageSettings = pageSettings;
if (!printDocument.PrinterSettings.IsValid)
throw new Exception("Can't find the default printer.");
else
{
printDocument.PrintPage += (sender, e) =>
{
Metafile pageImage = new Metafile(streams[pageIndex]);
Rectangle adjustedRect = new Rectangle(e.PageBounds.Left - (int)e.PageSettings.HardMarginX, e.PageBounds.Top - (int)e.PageSettings.HardMarginY, e.PageBounds.Width, e.PageBounds.Height);
e.Graphics.FillRectangle(Brushes.White, adjustedRect);
e.Graphics.DrawImage(pageImage, adjustedRect);
pageIndex++;
e.HasMorePages = (pageIndex < streams.Count);
e.Graphics.DrawRectangle(Pens.Red, adjustedRect);
};
printDocument.EndPrint += (Sender, e) =>
{
if (streams != null)
{
foreach (Stream stream in streams)
stream.Close();
streams = null;
}
};
printDocument.Print();
}
}
}
}
}
public void PrintSales(object sender, RenderingCompleteEventArgs e)
{
try
{
reportViewerSales.PageSetupDailog();
reportViewerSales.PrintDialog();
reportViewerSales.Clear();
reportViewerSales.LocalReport.ReleaseSandboxAppDomain();
}
catch (Exception ex)
{
}
}

'Emgu.CV.Invoke' threw an exception

I am relativity new EmguCV but I have done a lot of research and read a few tutorials and look through this site for the answer. All of the other people with similar problems are told to add the unmangaged dll to the output folder. I have done this and I am still getting this error 'Emgu.CV.Invoke' threw an exception.
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.VFW;
using System.Drawing.Imaging;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class SandBox : Form
{
private bool DeviceExist = false;
public VideoCaptureDevice FinalVideoSource;
public FilterInfoCollection VideoCaptureDevices;
private Capture capture;
private VideoWriter captureOutput;
private void FinalVideoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgseventArgs)
{
Bitmap image = (Bitmap)eventArgs.Frame.Clone();
image.RotateFlip(RotateFlipType.Rotate180FlipY);
pictureBox1.Image = image;
}
public SandBox()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = " ";
FPS.Text = " ";
try
{
VideoCaptureDevices = new FilterInfoCollection(AForge.Video.DirectShow.FilterCategory.VideoInputDevice);
DeviceExist = true;
foreach (AForge.Video.DirectShow.FilterInfo VideoCaptureDevice in VideoCaptureDevices)
{
comboBox1.Items.Add(VideoCaptureDevice.Name);
}
comboBox1.SelectedIndex = 0;
}
catch (ApplicationException)
{
DeviceExist = false;
comboBox1.Items.Add("No device on your system");
}
}
private void Start_Click(object sender, EventArgs e)
{
FinalVideoSource = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
FinalVideoSource.NewFrame += new NewFrameEventHandler(FinalVideoSource_NewFrame);
FinalVideoSource.Start();
label1.Text = "Device running...";
Start.Enabled = false;
Stop.Enabled = true;
timer1.Enabled = true;
capture = new Capture();
captureOutput = new VideoWriter("test.avi", 30, 1280, 720, true);
Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
captureOutput.WriteFrame(ImageFrame);
}
}
The error is being thrown in line capture = new Capture();. I cant figure out what is wrong with the project. Just to clarify I am only using EmguCV to record video not to display it on screen. Any help would be greatly appreciated.
The type initializer for 'Emgu.CV.CvInvoke' threw an exception its means that EMGU.CV file is not dectecting by code. this is due to system dependency. you need to go in to configration manger window in visual stdio and choose your required platform and then copy respective dll files(64 dll files forx64 and 32bit dll for x86 can be downloaded from emgu website ) according to your platform. copile it now there is no error like emgu.cv.invoke
http://www.emgu.com/wiki/index.php/Setting_up_EMGU_C_Sharp

Implement and trigger on detected motion

How can I implement a simple motion-detection method using EMGUCV? I have searched for applicable examples, but the only solutions I found were too complicated to implement.
Is there a way I can implement a simple method to detect motion in order to trigger something in my application?
Convert a single frame to grayscale.
Convert a new frames from real time into grayscale.
Make abstractions between the first frame and new frame from real time.
The result of this is a third, new frame comprised of the differences between the first two. Use erosion and thresholding for that to get a frame with white representing the motioned section and black representing the rest of the space.
Here is a piece of code:
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 Emgu.CV;
using Emgu.CV.UI;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System.Diagnostics;
using System.IO;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.Drawing.Imaging;
namespace ptuxiakh___
{
public partial class Form1 : Form
{
Capture _capture = new Capture();
Capture capture2 = new Capture();
Image<Bgr, Byte> FirstImage = new Image<Bgr, Byte>(640, 480);
Image<Bgr, Byte> RealTimeImage = new Image<Bgr, Byte>(640, 480);
Image<Gray, Byte> des = new Image<Gray, Byte>(640, 480);
Image<Gray, Byte> thres = new Image<Gray, Byte>(640, 480);
Image<Gray, Byte> eroded = new Image<Gray, Byte>(640, 480);
bool baground = false;
private void Background()
{
try{
FirstImage = _capture.QueryFrame();
background = true;
}
catch(Exception e)
{
baground = false;
}
}
private void Tracking(object sender, EventArgs e)
{
if (baground == true)
{
RealTimeImage = capture2.QueryFrame();
CvInvoke.cvAbsDiff(FirstImage.Convert<Gray, Byte>(), RealTimeImage.Convert<Gray, Byte>(), des);
CvInvoke.cvThreshold(des, thres, 20, 255, THRESH.CV_THRESH_BINARY);
CvInvoke.cvErode(thres, eroded, IntPtr.Zero, 2);
}
else
{
Background(); // At first trying to get a static frame for
// abstraction with real time frame
}
}
private void StartButton_Click(object sender, EventArgs e)
{
Application.Idle += new EventHandler(Tracking);
}
private void Stopbutton_Click(object sender, EventArgs e)
{
Application.Idle -= new EventHandler(Tracking);
}
//eroded will be your relust

Take image from video playing on windows media player in C#

In my windows application I use Windows Media Player dlls to play a video.
In my form I have a button to take a picture of the current video frame.
I did a lot of tests and code examinations but I couldn't find out why taking a picture of the current frame fails.
I tried this code, but the resulting image was black:
private Graphics g = null;
private void btnTakePicture_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(axWMVMovie.URL))
{
axWMVMovie.Ctlcontrols.pause();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
System.Drawing.Image ret = null;
try
{
Bitmap bitmap = new Bitmap(axWMVMovie.Width, axWMVMovie.Height);
{
g = Graphics.FromImage(bitmap);
{
Graphics gg = axWMVMovie.CreateGraphics();
{
timerTakePicFromVideo.Start();
}
}
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ret = System.Drawing.Image.FromStream(ms);
ret.Save(saveFileDialog1.FileName);
}
}
}
catch
{
}
}
}
}
private void timerTakePicFromVideo_Tick(object sender, EventArgs e)
{
timerTakePicFromVideo.Stop();
g.CopyFromScreen(axWMVMovie.PointToScreen(new System.Drawing.Point()).X,
axWMVMovie.PointToScreen(new System.Drawing.Point()).Y, 0, 0,
new System.Drawing.Size(axWMVMovie.Width, axWMVMovie.Height));
}
I use Timer because when the user selects the save path, function takes image from the file user specified in save file dialog. Video format is WMV.
I took your code and modified it. I put the code to capture the photo a little bit up and now it works. I create the picture right before the saveFileDialog pops up, so you will really get only the picture and not the saveFileDialog within your pic.
if (!string.IsNullOrEmpty(axWindowsMediaPlayer1.URL))
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
System.Drawing.Image ret = null;
try
{
// take picture BEFORE saveFileDialog pops up!!
Bitmap bitmap = new Bitmap(axWindowsMediaPlayer1.Width, axWindowsMediaPlayer1.Height);
{
Graphics g = Graphics.FromImage(bitmap);
{
Graphics gg = axWindowsMediaPlayer1.CreateGraphics();
{
//timerTakePicFromVideo.Start();
this.BringToFront();
g.CopyFromScreen(
axWindowsMediaPlayer1.PointToScreen(
new System.Drawing.Point()).X,
axWindowsMediaPlayer1.PointToScreen(
new System.Drawing.Point()).Y,
0, 0,
new System.Drawing.Size(
axWindowsMediaPlayer1.Width,
axWindowsMediaPlayer1.Height)
);
}
}
// afterwards save bitmap file if user wants to
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ret = System.Drawing.Image.FromStream(ms);
ret.Save(saveFileDialog1.FileName);
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
With previous answer a trick to avoid getting the controls on the capture is to do this before you capture :
string uimode_previus = axWindowsMediaPlayer2.uiMode;
axWindowsMediaPlayer2.uiMode = "none";
then when capture is done set the uimode back to previous like this :
axWindowsMediaPlayer2.uiMode = uimode_previus ;
In that way you only get the actual shoot from the current frame.
it is a little workaround but it does the job.
Here is working example
private void button8_Click_1(object sender, EventArgs e)
{
string uimode_previus = axWindowsMediaPlayer2.uiMode;
axWindowsMediaPlayer2.uiMode = "none";
if (!string.IsNullOrEmpty(axWindowsMediaPlayer2.URL))
{
ret = null;
try
{
// take picture BEFORE saveFileDialog pops up!!
Bitmap bitmap = new Bitmap(axWindowsMediaPlayer2.Width, axWindowsMediaPlayer2.Height);
{
Graphics g = Graphics.FromImage(bitmap);
{
Graphics gg = axWindowsMediaPlayer2.CreateGraphics();
{
//timerTakePicFromVideo.Start();
this.BringToFront();
g.CopyFromScreen(axWindowsMediaPlayer2.PointToScreen(
new System.Drawing.Point()).X,
axWindowsMediaPlayer2.PointToScreen(
new System.Drawing.Point()).Y,
0, 0,
new System.Drawing.Size(
axWindowsMediaPlayer2.Width - 0,
axWindowsMediaPlayer2.Height - 0)
);
}
}
// afterwards save bitmap file if user wants to
try
{
using (MemoryStream ms = new MemoryStream())
{
string rute = axWindowsMediaPlayer2.URL.ToString().Replace(".", "Review_."); //
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ret = new Bitmap(System.Drawing.Image.FromStream(ms));
ret.Save(rute.Replace(".mp4", ".Png"));
}
// open captured frame in new form
TeamEasy.ShowPictureForm spf = new ShowPictureForm();
spf.ImagePictureBox.Image = ret;
spf.ShowDialog();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
axWindowsMediaPlayer2.uiMode = uimode_previus;
// restore the UImode of player
}

A generic error occurred in GDI+ ExternalException Was caught cant figure out why

The error is on the line:
for (int x = 0; x < myList.Count(); x++)
The x++is painted with green.
Im using backgroundoworker and I used this code same code in another form before without a backgroundworker and it worked good. Now in the other form im using a click button event to show() this form and I want to use a progressBar1 to show the progress of the backgroundowrker work.
I used now try and catch inside this for loop and it went to the catch point and showed me the error. The full exception message is:
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at mws.Animation_Radar_Preview.backGroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in D:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Animation_Radar_Preview.cs:line 163
gifImages isnt null.
This is the full code of this form:
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.IO;
using DannyGeneral;
using unfreez_wrapper;
namespace mws
{
public partial class Animation_Radar_Preview : Form
{
int mtpStart;
int mtpEnd;
Picturebox1_Fullscreen pb1;
string radar_images_download_directory;
string tempRadarPngToGifDirectory;
int numberOfFiles;
UnFreezWrapper unfreez;
string path_exe;
List<string> myList;
string previewDirectory;
int animatedGifSpeed;
bool loop;
string nameOfStartFile;
string nameOfEndFile;
string previewFileName;
BackgroundWorker backGroundWorker1;
Image img;
private MemoryStream _memSt = null;
public Animation_Radar_Preview()
{
InitializeComponent();
mtpStart = Picturebox1_Fullscreen.mtp1Start;
mtpEnd = Picturebox1_Fullscreen.mtp1End;
animatedGifSpeed = Picturebox1_Fullscreen.animatedSpeed;
loop = Picturebox1_Fullscreen.looping;
pb1 = new Picturebox1_Fullscreen();
radar_images_download_directory = Options_DB.Get_Radar_Images_Download_Directory();
path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
tempRadarPngToGifDirectory = path_exe + "\\" + "tempRadarPngToGifDirectory";
if (Directory.Exists(tempRadarPngToGifDirectory))
{
}
else
{
Directory.CreateDirectory(tempRadarPngToGifDirectory);
}
previewDirectory = path_exe + "\\" + "previewDirectory";
if (Directory.Exists(previewDirectory))
{
}
else
{
Directory.CreateDirectory(previewDirectory);
}
previewFileName = previewDirectory + "\\" + "preview.gif";
loop = false;
animatedGifSpeed = 0;
unfreez = new UnFreezWrapper();
backGroundWorker1 = new BackgroundWorker();
backGroundWorker1.WorkerSupportsCancellation = true;
this.backGroundWorker1.WorkerReportsProgress = true;
backGroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backGroundWorker1_ProgressChanged);
backGroundWorker1.DoWork += new DoWorkEventHandler(backGroundWorker1_DoWork);
backGroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backGroundWorker1_RunWorkerCompleted);
backGroundWorker1.RunWorkerAsync();
progressBar1.Value = 0;
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult result1;
result1 = new DialogResult();
SaveFileDialog sd = new SaveFileDialog();
sd.Title = "Select a folder to save the animated gif to";
sd.InitialDirectory = "c:\\";
sd.FileName = null;
sd.Filter = "Gif File|*.gif;*.jpg|Gif|*.gif";
sd.FilterIndex = 1;
sd.RestoreDirectory = true;
result1 = sd.ShowDialog();
string file1 = sd.FileName;
if (result1 == DialogResult.OK)
{
File.Move(previewFileName, file1);
}
}
public void pictureBoxImage(string pbImage)
{
Image img2 = null;
try
{
using (img = Image.FromFile(pbImage))
{
//get the old image thats loaded from the _memSt memorystream
//and dispose it
Image i = this.pictureBox1.Image;
this.pictureBox1.Image = null;
if (i != null)
i.Dispose();
//grab the old stream
MemoryStream m = _memSt;
//save the new image to this stream
_memSt = new MemoryStream();
img.Save(_memSt, System.Drawing.Imaging.ImageFormat.Gif);
if (m != null)
m.Dispose();
//create our image to display
img2 = Image.FromStream(_memSt);
}
if (img2 != null)
pictureBox1.Image = img2;
label2.Text = numberOfFiles.ToString();
label6.Text = nameOfStartFile.ToString();
label4.Text = nameOfEndFile.ToString();
//File.Delete(pbImage);
}
catch(Exception err)
{
Logger.Write("Animation Error >>> " + err);
}
}
private void backGroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backGroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
List<string> myGifList;
Image gifImages = null;
//button1.Enabled = false;
Animation_Radar_Preview ap = new Animation_Radar_Preview();
//ap.FormClosing += new FormClosingEventHandler(ap_FormClosing);
FileInfo[] fi;
DirectoryInfo dir1 = new DirectoryInfo(radar_images_download_directory);
fi = dir1.GetFiles("*.png");
myList = new List<string>();
myGifList = new List<string>();
for (int i = mtpStart; i < mtpEnd; i++)
{
myList.Add(fi[i].FullName);
}
for (int x = 0; x < myList.Count(); x++)
{
try
{
gifImages = Image.FromFile(myList[x]);
gifImages.Save(tempRadarPngToGifDirectory + "\\" + x.ToString("D6") + ".Gif", System.Drawing.Imaging.ImageFormat.Gif);
}
catch (Exception ex)
{
MessageBox.Show(x.ToString() + "\r\n" + (gifImages == null).ToString() + "\r\n" + ex.Message);
}
}
myGifList = new List<string>();
dir1 = new DirectoryInfo(tempRadarPngToGifDirectory);
fi = dir1.GetFiles("*.gif");
nameOfStartFile = fi[0].Name;
for (int i = 0; i < fi.Length; i++)
{
myGifList.Add(fi[i].FullName);
//backGroundWorker1.ReportProgress(i);
nameOfEndFile = fi[i].Name.Length.ToString();
}
numberOfFiles = myGifList.Count();
unfreez.MakeGIF(myGifList, previewFileName, animatedGifSpeed, loop);
/*this.Invoke((MethodInvoker)delegate
{
pictureBoxImage(previewFileName);
});*/
}
private void backGroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pictureBoxImage(previewFileName);
}
}
}
I'm not entirely sure what is going wrong, especially as you say this same code worked previously — but it is not clear whether it worked with the same set of images.
Anyhow, the documentation for the Save method says that an ExternalException will be thrown in one of two situations:
Image was saved with the wrong format.
Image is saved to the file it was created from.
It cannot be that you are saving over the file because you are changing its extension, so it must be the wrong format. What this actually means is not clear. Perhaps one of the source images is using capabilities of the PNG format that cannot be converted to GIF, e.g. alpha channel transparency.
If I may take a moment to make some other suggestions too:
if (Directory.Exists(tempRadarPngToGifDirectory))
{
}
else
{
Directory.CreateDirectory(tempRadarPngToGifDirectory);
}
The empty 'success' case is not required if you invert the logic:
if (!Directory.Exists(tempRadarPngToGifDirectory)
{
Directory.CreateDirectory(tempRadarPngToGifDirectory);
}
Code that manipulates file paths such as the following:
tempRadarPngToGifDirectory = path_exe + "\\" + "tempRadarPngToGifDirectory";
You should consider using the Path class as this will handle the edge cases better and make more portable code:
tempRadarPngToGifDirectory = Path.Combine(path_exe, "tempRadarPngToGifDirectory");

Categories

Resources