private void Backup() { if (File.Exists(_path)) { if (_failOnExists) { throw new InstanceAlreadyExistsException(String.Format("DICOM File unexpectedly already exists: {0}", _path)); } try { _backupSpeed.Start(); var fi = new FileInfo(_path); _backupPath = FileUtils.Backup(_path, ProcessorContext.BackupDirectory); _backupSpeed.SetData(fi.Length); _backupSpeed.End(); } catch (IOException) { _backupPath = null; throw; } } }
protected override void OnExecute(CommandProcessor theProcessor) { if (_path == null) { String seriesUid = _file.DataSet[DicomTags.SeriesInstanceUid].GetString(0, String.Empty); String sopUid = _file.DataSet[DicomTags.SopInstanceUid].GetString(0, String.Empty); _path = _storageLocation.GetSopInstancePath(seriesUid, sopUid); } // Make sure the directory exists where we're storing the file. var p = Path.GetDirectoryName(_path); if (string.IsNullOrEmpty(p) || !Directory.Exists(p)) { if (!theProcessor.ExecuteSubCommand(this, new CreateDirectoryCommand(Path.GetDirectoryName(_path)))) { throw new ApplicationException(theProcessor.FailureReason); } } if (RequiresRollback) { Backup(); } string path = _saveTemp ? GetTempPath() : _path; using (FileStream stream = FileStreamOpener.OpenForSoleUpdate(path, FileMode.Create)) { // Set _fileCreated here, because the file has been opened. if (!_saveTemp) { _fileCreated = true; } _saveSpeed.Start(); _file.Save(stream, DicomWriteOptions.Default); stream.Flush(); stream.Close(); _saveSpeed.End(); var fi = new FileInfo(path); _saveSpeed.SetData(fi.Length); } if (_saveTemp) { if (File.Exists(_path)) { if (_failOnExists) { try { FileUtils.Delete(path); } catch (Exception x) { throw new ApplicationException(String.Format("DICOM File unexpectedly already exists: {0}", _path), x); } throw new ApplicationException(String.Format("DICOM File unexpectedly already exists: {0}", _path)); } FileUtils.Delete(_path); } File.Move(path, _path); _fileCreated = true; } }
private void Retrieve_Click(object sender, EventArgs e) { try { Uri baseUri = new Uri(String.Format("{0}/{1}", BaseUri.Text, ServerAE.Text)); StringBuilder url = new StringBuilder(); url.AppendFormat("{0}?requesttype=WADO&studyUID={1}&seriesUID={2}&objectUID={3}", baseUri, StudyUid.Text, SeriesUid.Text, ObjectUid.Text); if (UseFrame.Checked) { url.AppendFormat("&frameNumber={0}", int.Parse(Frame.Text)); } ContentTypes type = (ContentTypes)ContentTypes.SelectedItem; switch(type) { case ImageStreaming.ContentTypes.Dicom: url.AppendFormat("&ContentType={0}", "application/dicom"); break; case ImageStreaming.ContentTypes.RawPixel: url.AppendFormat("&ContentType={0}", "application/clearcanvas"); break; case ImageStreaming.ContentTypes.NotSpecified: break; } RateStatistics speed = new RateStatistics("Speed", RateType.BYTES); speed.Start(); HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url.ToString()); request.Accept = "application/dicom,application/clearcanvas"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { throw new Exception(String.Format("Server responded with an error: {0}", HttpUtility.HtmlDecode(response.StatusDescription))); } byte[] buffer = new byte[response.ContentLength]; Stream stream = response.GetResponseStream(); int offset = 0; do { int readSize = stream.Read(buffer, offset, buffer.Length - offset); if (readSize <= 0) break; offset += readSize; } while (true); stream.Close(); speed.SetData(buffer.Length); speed.End(); String msg = String.Format("Mime:\t{0}\nSize:\t{1}\nSpeed:\t{2}", response.ContentType,ByteCountFormatter.Format((ulong)buffer.Length),speed.FormattedValue); MessageBox.Show(msg); } catch(WebException ex) { HttpWebResponse rsp = (ex.Response as HttpWebResponse); if (rsp != null) { string msg = String.Format("Error: {0}\n{1}", rsp.StatusCode, HttpUtility.HtmlDecode(rsp.StatusDescription) ); MessageBox.Show(msg); } } }
private void Retrieve_Click(object sender, EventArgs e) { try { Uri baseUri = new Uri(String.Format("{0}/{1}", BaseUri.Text, ServerAE.Text)); StringBuilder url = new StringBuilder(); url.AppendFormat("{0}?requesttype=WADO&studyUID={1}&seriesUID={2}&objectUID={3}", baseUri, StudyUid.Text, SeriesUid.Text, ObjectUid.Text); if (UseFrame.Checked) { url.AppendFormat("&frameNumber={0}", int.Parse(Frame.Text)); } ContentTypes type = (ContentTypes)ContentTypes.SelectedItem; switch (type) { case ImageStreaming.ContentTypes.Dicom: url.AppendFormat("&ContentType={0}", "application/dicom"); break; case ImageStreaming.ContentTypes.RawPixel: url.AppendFormat("&ContentType={0}", "application/clearcanvas"); break; case ImageStreaming.ContentTypes.NotSpecified: break; } RateStatistics speed = new RateStatistics("Speed", RateType.BYTES); speed.Start(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString()); request.Accept = "application/dicom,application/clearcanvas"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { throw new Exception(String.Format("Server responded with an error: {0}", HttpUtility.HtmlDecode(response.StatusDescription))); } byte[] buffer = new byte[response.ContentLength]; Stream stream = response.GetResponseStream(); int offset = 0; do { int readSize = stream.Read(buffer, offset, buffer.Length - offset); if (readSize <= 0) { break; } offset += readSize; } while (true); stream.Close(); speed.SetData(buffer.Length); speed.End(); String msg = String.Format("Mime:\t{0}\nSize:\t{1}\nSpeed:\t{2}", response.ContentType, ByteCountFormatter.Format((ulong)buffer.Length), speed.FormattedValue); MessageBox.Show(msg); } catch (WebException ex) { HttpWebResponse rsp = (ex.Response as HttpWebResponse); if (rsp != null) { string msg = String.Format("Error: {0}\n{1}", rsp.StatusCode, HttpUtility.HtmlDecode(rsp.StatusDescription) ); MessageBox.Show(msg); } } }
private static void RetrieveImages(string serverAE, string studyPath) { Console.WriteLine("server={0}", serverAE); string baseUri = String.Format("http://{0}:{1}", serverHost, serverPort); StreamingClient client = new StreamingClient(new Uri(baseUri)); int totalFrameCount = 0; DirectoryInfo directoryInfo = new DirectoryInfo(studyPath); string studyUid = directoryInfo.Name; RateStatistics frameRate = new RateStatistics("Speed", "frame"); RateStatistics speed = new RateStatistics("Speed", RateType.BYTES); AverageRateStatistics averageSpeed = new AverageRateStatistics(RateType.BYTES); ByteCountStatistics totalSize = new ByteCountStatistics("Size"); frameRate.Start(); speed.Start(); Console.WriteLine("\n------------------------------------------------------------------------------------------------------------------------"); string[] seriesDirs = Directory.GetDirectories(studyPath); foreach (string seriesPath in seriesDirs) { DirectoryInfo dirInfo = new DirectoryInfo(seriesPath); string seriesUid = dirInfo.Name; string[] objectUidPath = Directory.GetFiles(seriesPath, "*.dcm"); foreach (string uidPath in objectUidPath) { FileInfo fileInfo = new FileInfo(uidPath); string uid = fileInfo.Name.Replace(".dcm", ""); Console.Write("{0,-64}... ", uid); try { Stream imageStream; StreamingResultMetaData imageMetaData; FrameStreamingResultMetaData frameMetaData; switch (type) { case ContentTypes.Dicom: imageStream = client.RetrieveImage(serverAE, studyUid, seriesUid, uid, out imageMetaData); totalFrameCount++; averageSpeed.AddSample(imageMetaData.Speed); totalSize.Value += (ulong)imageMetaData.ContentLength; Console.WriteLine("1 dicom sop [{0,10}] in {1,12}\t[mime={2}]", ByteCountFormatter.Format((ulong)imageStream.Length), TimeSpanFormatter.Format(imageMetaData.Speed.ElapsedTime), imageMetaData.ResponseMimeType); break; case ContentTypes.RawPixel: TimeSpanStatistics elapsedTime = new TimeSpanStatistics(); elapsedTime.Start(); ulong instanceSize = 0; int frameCount = 0; do { RetrievePixelDataResult result = client.RetrievePixelData(serverAE, studyUid, seriesUid, uid, frameCount); frameMetaData = result.MetaData; totalFrameCount++; frameCount++; averageSpeed.AddSample(frameMetaData.Speed); totalSize.Value += (ulong)frameMetaData.ContentLength; instanceSize += (ulong)frameMetaData.ContentLength; } while (!frameMetaData.IsLast); elapsedTime.End(); Console.WriteLine("{0,3} frame(s) [{1,10}] in {2,12}\t[mime={3}]", frameCount, ByteCountFormatter.Format(instanceSize), elapsedTime.FormattedValue, frameMetaData.ResponseMimeType); break; default: imageStream = client.RetrieveImage(serverAE, studyUid, seriesUid, uid, out imageMetaData); totalFrameCount++; averageSpeed.AddSample(imageMetaData.Speed); totalSize.Value += (ulong)imageMetaData.ContentLength; Console.WriteLine("1 object [{0,10}] in {1,12}\t[mime={2}]", ByteCountFormatter.Format((ulong)imageStream.Length), TimeSpanFormatter.Format(imageMetaData.Speed.ElapsedTime), imageMetaData.ResponseMimeType); break; } } catch (Exception ex) { if (ex is WebException) { HttpWebResponse rsp = ((ex as WebException).Response as HttpWebResponse); string msg = String.Format("Error: {0} : {1}", rsp.StatusCode, HttpUtility.HtmlDecode(rsp.StatusDescription) ); Console.WriteLine(msg); } else { Console.WriteLine(ex.Message); } } } } frameRate.SetData(totalFrameCount); frameRate.End(); speed.SetData(totalSize.Value); speed.End(); Console.WriteLine("\nTotal {0,3} image(s)/frame(s) [{1,10}] in {2,12} ==> [ Speed: {3,12} or {4,12}]", totalFrameCount, totalSize.FormattedValue, TimeSpanFormatter.Format(frameRate.ElapsedTime), frameRate.FormattedValue, speed.FormattedValue ); }
private static void RetrieveImages(string serverAE, string studyPath) { Console.WriteLine("server={0}", serverAE); string baseUri = String.Format("http://{0}:{1}", serverHost, serverPort); StreamingClient client = new StreamingClient(new Uri(baseUri)); int totalFrameCount = 0; DirectoryInfo directoryInfo = new DirectoryInfo(studyPath); string studyUid = directoryInfo.Name; RateStatistics frameRate = new RateStatistics("Speed", "frame"); RateStatistics speed = new RateStatistics("Speed", RateType.BYTES); AverageRateStatistics averageSpeed = new AverageRateStatistics(RateType.BYTES); ByteCountStatistics totalSize = new ByteCountStatistics("Size"); frameRate.Start(); speed.Start(); Console.WriteLine("\n------------------------------------------------------------------------------------------------------------------------"); string[] seriesDirs = Directory.GetDirectories(studyPath); foreach(string seriesPath in seriesDirs) { DirectoryInfo dirInfo = new DirectoryInfo(seriesPath); string seriesUid = dirInfo.Name; string[] objectUidPath = Directory.GetFiles(seriesPath, "*.dcm"); foreach (string uidPath in objectUidPath) { FileInfo fileInfo = new FileInfo(uidPath); string uid = fileInfo.Name.Replace(".dcm", ""); Console.Write("{0,-64}... ", uid); try { Stream imageStream; StreamingResultMetaData imageMetaData; FrameStreamingResultMetaData frameMetaData; switch(type) { case ContentTypes.Dicom: imageStream = client.RetrieveImage(serverAE, studyUid, seriesUid, uid, out imageMetaData); totalFrameCount++; averageSpeed.AddSample(imageMetaData.Speed); totalSize.Value += (ulong)imageMetaData.ContentLength; Console.WriteLine("1 dicom sop [{0,10}] in {1,12}\t[mime={2}]", ByteCountFormatter.Format((ulong)imageStream.Length), TimeSpanFormatter.Format(imageMetaData.Speed.ElapsedTime), imageMetaData.ResponseMimeType); break; case ContentTypes.RawPixel: TimeSpanStatistics elapsedTime = new TimeSpanStatistics(); elapsedTime.Start(); ulong instanceSize = 0; int frameCount = 0; do { RetrievePixelDataResult result = client.RetrievePixelData(serverAE, studyUid, seriesUid, uid, frameCount); frameMetaData = result.MetaData; totalFrameCount++; frameCount++; averageSpeed.AddSample(frameMetaData.Speed); totalSize.Value += (ulong)frameMetaData.ContentLength; instanceSize += (ulong)frameMetaData.ContentLength; } while (!frameMetaData.IsLast); elapsedTime.End(); Console.WriteLine("{0,3} frame(s) [{1,10}] in {2,12}\t[mime={3}]", frameCount, ByteCountFormatter.Format(instanceSize), elapsedTime.FormattedValue, frameMetaData.ResponseMimeType); break; default: imageStream = client.RetrieveImage(serverAE, studyUid, seriesUid, uid, out imageMetaData); totalFrameCount++; averageSpeed.AddSample(imageMetaData.Speed); totalSize.Value += (ulong)imageMetaData.ContentLength; Console.WriteLine("1 object [{0,10}] in {1,12}\t[mime={2}]", ByteCountFormatter.Format((ulong)imageStream.Length), TimeSpanFormatter.Format(imageMetaData.Speed.ElapsedTime), imageMetaData.ResponseMimeType); break; } } catch(Exception ex) { if (ex is WebException) { HttpWebResponse rsp = ( (ex as WebException).Response as HttpWebResponse); string msg = String.Format("Error: {0} : {1}", rsp.StatusCode,HttpUtility.HtmlDecode(rsp.StatusDescription) ); Console.WriteLine(msg); } else { Console.WriteLine(ex.Message); } } } } frameRate.SetData(totalFrameCount); frameRate.End(); speed.SetData(totalSize.Value); speed.End(); Console.WriteLine("\nTotal {0,3} image(s)/frame(s) [{1,10}] in {2,12} ==> [ Speed: {3,12} or {4,12}]", totalFrameCount, totalSize.FormattedValue, TimeSpanFormatter.Format(frameRate.ElapsedTime), frameRate.FormattedValue, speed.FormattedValue ); }