/// <summary> /// 对指定的RenderWindowControl进行截屏并保存 /// </summary> /// <param name="fn"></param> /// <param name="rwc"></param> /// <returns></returns> public static bool SaveRendererWindowsAsPic(string fn, ref RenderWindowControl rwc) { bool res = true; try { vtkWindowToImageFilter screenShot = vtkWindowToImageFilter.New(); screenShot.SetInput(rwc.RenderWindow); //screenShot.SetMagnification(3); screenShot.SetInputBufferTypeToRGB(); screenShot.ReadFrontBufferOff(); screenShot.Update(); vtkPNGWriter writer = vtkPNGWriter.New(); writer.SetFileName(fn); writer.SetInputConnection(screenShot.GetOutputPort()); writer.Write(); } catch { res = false; } return(res); }
private void WritePNG() { // Path to vtk data must be set as an environment variable // VTK_DATA_ROOT = "C:\VTK\vtkdata-5.8.0" vtkTesting test = vtkTesting.New(); string root = test.GetDataRoot(); string filePath = System.IO.Path.Combine(root, @"Data\test_png.png"); int[] extent = new int[] { 0, 99, 0, 99, 0, 0 }; vtkImageCanvasSource2D imageSource = vtkImageCanvasSource2D.New(); imageSource.SetExtent(extent[0], extent[1], extent[2], extent[3], extent[4], extent[5]); imageSource.SetScalarTypeToUnsignedChar(); imageSource.SetNumberOfScalarComponents(3); imageSource.SetDrawColor(127, 45, 255); imageSource.FillBox(0, 99, 0, 99); imageSource.SetDrawColor(255, 255, 255); imageSource.FillBox(40, 70, 20, 50); imageSource.Update(); vtkImageCast castFilter = vtkImageCast.New(); castFilter.SetOutputScalarTypeToUnsignedChar(); castFilter.SetInputConnection(imageSource.GetOutputPort()); castFilter.Update(); vtkPNGWriter writer = vtkPNGWriter.New(); writer.SetFileName(filePath); writer.SetInputConnection(castFilter.GetOutputPort()); writer.Write(); // Read and display file for verification that it was written correctly vtkPNGReader reader = vtkPNGReader.New(); if (reader.CanReadFile(filePath) == 0) { MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK); return; } reader.SetFileName(filePath); reader.Update(); // Visualize vtkImageViewer2 imageViewer = vtkImageViewer2.New(); imageViewer.SetInputConnection(reader.GetOutputPort()); // get a reference to the renderwindow of our renderWindowControl1 vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow; // renderer vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer(); // set background color renderer.SetBackground(0.2, 0.3, 0.4); imageViewer.SetRenderer(renderer); renderer.ResetCamera(); }
private void WritePNG(string filePath) { vtkRenderWindow renW = myRenderWindowControl.RenderWindow; vtkRenderer ren = renW.GetRenderers().GetFirstRenderer(); double [] backCol; backCol = ren.GetBackground(); ren.SetBackground(255.0, 255.0, 255.0); vtkRenderLargeImage largeScreenExpoHelper = vtkRenderLargeImage.New(); largeScreenExpoHelper.SetInput(ren); largeScreenExpoHelper.SetMagnification(10); vtkPNGWriter writer = vtkPNGWriter.New(); writer.SetFileName(filePath); writer.SetInputConnection(largeScreenExpoHelper.GetOutputPort()); writer.Write(); ren.SetBackground(backCol[0], backCol[1], backCol[2]); }
/// <summary> /// The main entry method called by the CSharp driver /// </summary> /// <param name="argv"></param> public static void AVTestImageWriters(String [] argv) { //Prefix Content is: "" // Image pipeline[] image1 = new vtkTIFFReader(); image1.SetFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/beach.tif"); // "beach.tif" image contains ORIENTATION tag which is [] // ORIENTATION_TOPLEFT (row 0 top, col 0 lhs) type. The TIFF [] // reader parses this tag and sets the internal TIFF image [] // orientation accordingly. To overwrite this orientation with a vtk[] // convention of ORIENTATION_BOTLEFT (row 0 bottom, col 0 lhs ), invoke[] // SetOrientationType method with parameter value of 4.[] image1.SetOrientationType((uint)4); image1.Update(); sp = new vtkStructuredPoints(); sp.SetDimensions(image1.GetOutput().GetDimensions()[0], image1.GetOutput().GetDimensions()[1], image1.GetOutput().GetDimensions()[2]); sp.SetExtent( image1.GetOutput().GetExtent()[0], image1.GetOutput().GetExtent()[1], image1.GetOutput().GetExtent()[2], image1.GetOutput().GetExtent()[3], image1.GetOutput().GetExtent()[4], image1.GetOutput().GetExtent()[5]); //sp.SetScalarType((int)image1.GetOutput().GetScalarType()); //sp.SetNumberOfScalarComponents((int)image1.GetOutput().GetNumberOfScalarComponents()); vtkDataObject.SetPointDataActiveScalarInfo(sp.GetInformation(), (int)image1.GetOutput().GetScalarType(), (int)image1.GetOutput().GetNumberOfScalarComponents()); sp.GetPointData().SetScalars((vtkDataArray)image1.GetOutput().GetPointData().GetScalars()); luminance = new vtkImageLuminance(); luminance.SetInputData((vtkDataObject)sp); //[] // write to the temp directory if possible, otherwise use .[] //[] dir = "."; dir = TclToCsScriptTestDriver.GetTempDirectory(); // make sure it is writeable first[] try { channel = new StreamWriter("" + (dir.ToString()) + "/test.tmp"); tryCatchError = "NOERROR"; } catch (Exception) { tryCatchError = "ERROR"; } if (tryCatchError.Equals("NOERROR")) { channel.Close(); File.Delete("" + (dir.ToString()) + "/test.tmp"); tiff1 = new vtkTIFFWriter(); tiff1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort()); tiff1.SetFileName((string)"" + (dir.ToString()) + "/tiff1.tif"); tiff2 = new vtkTIFFWriter(); tiff2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); tiff2.SetFileName((string)"" + (dir.ToString()) + "/tiff2.tif"); bmp1 = new vtkBMPWriter(); bmp1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort()); bmp1.SetFileName((string)"" + (dir.ToString()) + "/bmp1.bmp"); bmp2 = new vtkBMPWriter(); bmp2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); bmp2.SetFileName((string)"" + (dir.ToString()) + "/bmp2.bmp"); pnm1 = new vtkPNMWriter(); pnm1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort()); pnm1.SetFileName((string)"" + (dir.ToString()) + "/pnm1.pnm"); pnm2 = new vtkPNMWriter(); pnm2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); pnm2.SetFileName((string)"" + (dir.ToString()) + "/pnm2.pnm"); psw1 = new vtkPostScriptWriter(); psw1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort()); psw1.SetFileName((string)"" + (dir.ToString()) + "/psw1.ps"); psw2 = new vtkPostScriptWriter(); psw2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); psw2.SetFileName((string)"" + (dir.ToString()) + "/psw2.ps"); pngw1 = new vtkPNGWriter(); pngw1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort()); pngw1.SetFileName((string)"" + (dir.ToString()) + "/pngw1.png"); pngw2 = new vtkPNGWriter(); pngw2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); pngw2.SetFileName((string)"" + (dir.ToString()) + "/pngw2.png"); jpgw1 = new vtkJPEGWriter(); jpgw1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort()); jpgw1.SetFileName((string)"" + (dir.ToString()) + "/jpgw1.jpg"); jpgw2 = new vtkJPEGWriter(); jpgw2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); jpgw2.SetFileName((string)"" + (dir.ToString()) + "/jpgw2.jpg"); tiff1.Write(); tiff2.Write(); bmp1.Write(); bmp2.Write(); pnm1.Write(); pnm2.Write(); psw1.Write(); psw2.Write(); pngw1.Write(); pngw2.Write(); jpgw1.Write(); jpgw2.Write(); File.Delete("" + (dir.ToString()) + "/tiff1.tif"); File.Delete("" + (dir.ToString()) + "/tiff2.tif"); File.Delete("" + (dir.ToString()) + "/bmp1.bmp"); File.Delete("" + (dir.ToString()) + "/bmp2.bmp"); File.Delete("" + (dir.ToString()) + "/pnm1.pnm"); File.Delete("" + (dir.ToString()) + "/pnm2.pnm"); File.Delete("" + (dir.ToString()) + "/psw1.ps"); File.Delete("" + (dir.ToString()) + "/psw2.ps"); File.Delete("" + (dir.ToString()) + "/pngw1.png"); File.Delete("" + (dir.ToString()) + "/pngw2.png"); File.Delete("" + (dir.ToString()) + "/jpgw1.jpg"); File.Delete("" + (dir.ToString()) + "/jpgw2.jpg"); } viewer = new vtkImageViewer(); viewer.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); viewer.SetColorWindow((double)255); viewer.SetColorLevel((double)127.5); viewer.Render(); //deleteAllVTKObjects(); }
/// <summary> /// The main entry method called by the CSharp driver /// </summary> /// <param name="argv"></param> public static void AVTestImageWriters(String [] argv) { //Prefix Content is: "" // Image pipeline[] image1 = new vtkTIFFReader(); image1.SetFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/beach.tif"); // "beach.tif" image contains ORIENTATION tag which is [] // ORIENTATION_TOPLEFT (row 0 top, col 0 lhs) type. The TIFF [] // reader parses this tag and sets the internal TIFF image [] // orientation accordingly. To overwrite this orientation with a vtk[] // convention of ORIENTATION_BOTLEFT (row 0 bottom, col 0 lhs ), invoke[] // SetOrientationType method with parameter value of 4.[] image1.SetOrientationType((uint)4); image1.Update(); sp = new vtkStructuredPoints(); sp.SetDimensions(image1.GetOutput().GetDimensions()[0],image1.GetOutput().GetDimensions()[1],image1.GetOutput().GetDimensions()[2]); sp.SetExtent( image1.GetOutput().GetExtent()[0], image1.GetOutput().GetExtent()[1], image1.GetOutput().GetExtent()[2], image1.GetOutput().GetExtent()[3], image1.GetOutput().GetExtent()[4], image1.GetOutput().GetExtent()[5]); //sp.SetScalarType((int)image1.GetOutput().GetScalarType()); //sp.SetNumberOfScalarComponents((int)image1.GetOutput().GetNumberOfScalarComponents()); vtkDataObject.SetPointDataActiveScalarInfo(sp.GetInformation(), (int)image1.GetOutput().GetScalarType(), (int)image1.GetOutput().GetNumberOfScalarComponents()); sp.GetPointData().SetScalars((vtkDataArray)image1.GetOutput().GetPointData().GetScalars()); luminance = new vtkImageLuminance(); luminance.SetInputData((vtkDataObject)sp); //[] // write to the temp directory if possible, otherwise use .[] //[] dir = "."; dir = TclToCsScriptTestDriver.GetTempDirectory(); // make sure it is writeable first[] try { channel = new StreamWriter("" + (dir.ToString()) + "/test.tmp"); tryCatchError = "NOERROR"; } catch(Exception) {tryCatchError = "ERROR";} if(tryCatchError.Equals("NOERROR")) { channel.Close(); File.Delete("" + (dir.ToString()) + "/test.tmp"); tiff1 = new vtkTIFFWriter(); tiff1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort()); tiff1.SetFileName((string)"" + (dir.ToString()) + "/tiff1.tif"); tiff2 = new vtkTIFFWriter(); tiff2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); tiff2.SetFileName((string)"" + (dir.ToString()) + "/tiff2.tif"); bmp1 = new vtkBMPWriter(); bmp1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort()); bmp1.SetFileName((string)"" + (dir.ToString()) + "/bmp1.bmp"); bmp2 = new vtkBMPWriter(); bmp2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); bmp2.SetFileName((string)"" + (dir.ToString()) + "/bmp2.bmp"); pnm1 = new vtkPNMWriter(); pnm1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort()); pnm1.SetFileName((string)"" + (dir.ToString()) + "/pnm1.pnm"); pnm2 = new vtkPNMWriter(); pnm2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); pnm2.SetFileName((string)"" + (dir.ToString()) + "/pnm2.pnm"); psw1 = new vtkPostScriptWriter(); psw1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort()); psw1.SetFileName((string)"" + (dir.ToString()) + "/psw1.ps"); psw2 = new vtkPostScriptWriter(); psw2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); psw2.SetFileName((string)"" + (dir.ToString()) + "/psw2.ps"); pngw1 = new vtkPNGWriter(); pngw1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort()); pngw1.SetFileName((string)"" + (dir.ToString()) + "/pngw1.png"); pngw2 = new vtkPNGWriter(); pngw2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); pngw2.SetFileName((string)"" + (dir.ToString()) + "/pngw2.png"); jpgw1 = new vtkJPEGWriter(); jpgw1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort()); jpgw1.SetFileName((string)"" + (dir.ToString()) + "/jpgw1.jpg"); jpgw2 = new vtkJPEGWriter(); jpgw2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); jpgw2.SetFileName((string)"" + (dir.ToString()) + "/jpgw2.jpg"); tiff1.Write(); tiff2.Write(); bmp1.Write(); bmp2.Write(); pnm1.Write(); pnm2.Write(); psw1.Write(); psw2.Write(); pngw1.Write(); pngw2.Write(); jpgw1.Write(); jpgw2.Write(); File.Delete("" + (dir.ToString()) + "/tiff1.tif"); File.Delete("" + (dir.ToString()) + "/tiff2.tif"); File.Delete("" + (dir.ToString()) + "/bmp1.bmp"); File.Delete("" + (dir.ToString()) + "/bmp2.bmp"); File.Delete("" + (dir.ToString()) + "/pnm1.pnm"); File.Delete("" + (dir.ToString()) + "/pnm2.pnm"); File.Delete("" + (dir.ToString()) + "/psw1.ps"); File.Delete("" + (dir.ToString()) + "/psw2.ps"); File.Delete("" + (dir.ToString()) + "/pngw1.png"); File.Delete("" + (dir.ToString()) + "/pngw2.png"); File.Delete("" + (dir.ToString()) + "/jpgw1.jpg"); File.Delete("" + (dir.ToString()) + "/jpgw2.jpg"); } viewer = new vtkImageViewer(); viewer.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort()); viewer.SetColorWindow((double)255); viewer.SetColorLevel((double)127.5); viewer.Render(); //deleteAllVTKObjects(); }
private void buttonSaveCurrentPlane_Click(object sender, EventArgs e) { //make dir Random RandNum = new Random(); string HashForDir = checkMD5(DateTime.Now.ToLongDateString() + Convert.ToString(RandNum.Next(1111, 9999))); Directory.CreateDirectory(Program.TempDir + "\\" + HashForDir); StreamWriter IniFileInDir = File.CreateText(Program.TempDir + "\\" + HashForDir + "\\Ini.ini"); IniFileInDir.WriteLine("[Info]"); IniFileInDir.WriteLine(@"temp_folder=" + Program.TempDir + "\n" + "max_scalar = \"\"\n" + "min_scalar = \"\"\n" + @"dicom_folder =" + Path.GetDirectoryName(openDicomFileDialog.FileName)); IniFileInDir.Close(); Program.IniObj.IniWriteValue("Info", "curr_work_dir", HashForDir); Ini localIni = new Ini(Program.TempDir + "\\" + HashForDir + "\\Ini.ini"); //save scalar data of selected image StreamWriter streamWriter = new StreamWriter(Program.TempDir + "\\" + HashForDir + "\\" + "imageScalars.txt"); for (int i = 0; i < 500; i++) { for (int j = 0; j < 500; j++) { streamWriter.Write(Convert.ToString( SiteplanesObj.planeWidgetZ.GetResliceOutput().GetScalarComponentAsDouble(i, j, 0, 0) ) + " "); } streamWriter.Write("\n"); } streamWriter.Close(); //save image in png file vtkWindowToImageFilter imf = new vtkWindowToImageFilter(); imf.SetInputBufferTypeToRGBA(); switch (currentSliceDimension) { case 1: { imf.SetInput(vtkFormsWindowControlX.GetRenderWindow()); localIni.IniWriteValue("Info", "max_scalar", Convert.ToString(SiteplanesObj.rangeXMax)); localIni.IniWriteValue("Info", "min_scalar", Convert.ToString(SiteplanesObj.rangeXMin)); break; } case 2: { imf.SetInput(vtkFormsWindowControlY.GetRenderWindow()); localIni.IniWriteValue("Info", "max_scalar", Convert.ToString(SiteplanesObj.rangeYMax)); localIni.IniWriteValue("Info", "min_scalar", Convert.ToString(SiteplanesObj.rangeYMin)); break; } case 3: { imf.SetInput(vtkFormsWindowControlZ.GetRenderWindow()); localIni.IniWriteValue("Info", "max_scalar", Convert.ToString(SiteplanesObj.rangeZMax)); localIni.IniWriteValue("Info", "min_scalar", Convert.ToString(SiteplanesObj.rangeZMin)); break; } } vtkPNGWriter pngw = new vtkPNGWriter(); pngw.SetFileName(Program.TempDir + "\\" + HashForDir + "\\" + "\\image.png"); pngw.SetInputConnection(imf.GetOutputPort()); pngw.Write(); System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory + "imagePreprocessing.exe"); /*get CT values from * SaveFileDialog saveFileDialog1 = new SaveFileDialog(); * * saveFileDialog1.Filter = "jpg files (*.jpg)|*.jpg|png files (*.png)|*.png|tif files (*.tif)|*.tif|tiff files (*.tiff)|*.tiff"; * saveFileDialog1.FilterIndex = 2; * * if (saveFileDialog1.ShowDialog() == DialogResult.OK) * { * * * * //get CT values from current slice * StreamWriter streamWriter = new StreamWriter(Path.GetDirectoryName(saveFileDialog1.FileName) + "\\" + "imageScalars.txt"); * for(int i = 0; i < 500; i++) * { * for(int j = 0; j < 500; j++) * { * streamWriter.Write(Convert.ToString( * SiteplanesObj.planeWidgetZ.GetResliceOutput().GetScalarComponentAsDouble(i, j, 0, 0) * ) + " "); * } * streamWriter.Write("\n"); * } * streamWriter.Close(); * //get CT values from current slice * * * * vtkWindowToImageFilter imf = new vtkWindowToImageFilter(); * imf.SetInputBufferTypeToRGBA(); * switch (currentSliceDimension) * { * case 1: * { * imf.SetInput(vtkFormsWindowControlX.GetRenderWindow()); * break; * } * case 2: * { * imf.SetInput(vtkFormsWindowControlY.GetRenderWindow()); * break; * } * case 3: * { * imf.SetInput(vtkFormsWindowControlZ.GetRenderWindow()); * break; * } * } * * * var estns = Path.GetExtension(saveFileDialog1.FileName); * if (estns == ".jpg") * { * vtkJPEGWriter pngw = new vtkJPEGWriter(); * pngw.SetFileName(saveFileDialog1.FileName); * pngw.SetInputConnection(imf.GetOutputPort()); * pngw.Write(); * } * else if (estns == ".png") * { * vtkPNGWriter pngw = new vtkPNGWriter(); * pngw.SetFileName(saveFileDialog1.FileName); * pngw.SetInputConnection(imf.GetOutputPort()); * pngw.Write(); * } * else if (estns == ".tif" || estns == ".tiff") * { * vtkTIFFWriter pngw = new vtkTIFFWriter(); * pngw.SetFileName(saveFileDialog1.FileName); * pngw.SetInputConnection(imf.GetOutputPort()); * pngw.Write(); * } * * }*/ }