private static IImageEncoder GetEncoder(ResizeOptions options, IImageFormat?format) { var encoder = Configuration.Default.ImageFormatsManager.FindEncoder(format); if (encoder == null) { throw new NotSupportedException(); } if (options.Quality.HasValue && (encoder is JpegEncoder || !options.KeepFormat) && options.Format == ImageFormat.Auto) { encoder = new JpegEncoder { Quality = options.Quality.Value }; } else if (options.Format == ImageFormat.JPEG) { encoder = new JpegEncoder(); } else if (options.Format == ImageFormat.PNG) { encoder = new PngEncoder(); } else if (options.Format == ImageFormat.TGA) { encoder = new TgaEncoder(); } else if (options.Format == ImageFormat.GIF) { encoder = new GifEncoder(); } return(encoder); }
private static void TestTgaEncoderCore <TPixel>( TestImageProvider <TPixel> provider, TgaBitsPerPixel bitsPerPixel, TgaCompression compression = TgaCompression.None, bool useExactComparer = true, float compareTolerance = 0.01f) where TPixel : unmanaged, IPixel <TPixel> { using (Image <TPixel> image = provider.GetImage()) { var encoder = new TgaEncoder { BitsPerPixel = bitsPerPixel, Compression = compression }; using (var memStream = new MemoryStream()) { image.Save(memStream, encoder); memStream.Position = 0; using (var encodedImage = (Image <TPixel>)Image.Load(memStream)) { TgaTestUtils.CompareWithReferenceDecoder(provider, encodedImage, useExactComparer, compareTolerance); } } } }
public override async Task ConvertAsync(IList <Image <Rgba32> > images, Stream outputStream, CancellationToken cancellationToken) { if (images is null) { throw new ArgumentNullException(nameof(images)); } if (images.Count != 1) { throw new ArgumentException("Only one image is permitted for this format.", nameof(images)); } if (outputStream is null) { throw new ArgumentNullException(nameof(outputStream)); } var image = images[0]; Resize(image); var encoder = new TgaEncoder { BitsPerPixel = TgaBitsPerPixel.Pixel32, Compression = TgaCompression.None, }; await image.SaveAsync(outputStream, encoder, cancellationToken); }
private byte[] GetImageData() { if (PageOpenImageControl.WorkspaceViewer.Image == null) { return(null); } AtalaImage atalaImage = null; if (PageOpenImageControl.WorkspaceViewer.Image.PixelFormat == PixelFormat.Pixel32bppBgra) { atalaImage = PageOpenImageControl.WorkspaceViewer.Image; } else { atalaImage = PageOpenImageControl.WorkspaceViewer.Image.GetChangedPixelFormat(PixelFormat.Pixel32bppBgra); } byte[] memoryBuffer = new byte[atalaImage.Width * atalaImage.Height * 4 + 1042 + 18]; MemoryStream memoryStream = new MemoryStream(memoryBuffer); TgaEncoder tgaEncoder = new TgaEncoder(); tgaEncoder.Save(memoryStream, atalaImage, null); return(memoryBuffer); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void axPushButton_ClickEvent(object sender, EventArgs e) { MessageBox.Show("解压的可能时间比较长 请等待分钟 或更长的时间"); if (m_FolderBrowserDialog.ShowDialog() == DialogResult.OK) { for (int i = 0; i < m_SGLEditorDocumentForm.m_SglFileInfo.SGLImage.Length; i++) { SGLImage sglImage = m_SGLEditorDocumentForm.m_SglFileInfo.SGLImage[i]; for (int i2 = 0; i2 < sglImage.SGLFrames.Length; i2++) { SGLFrame sglFrame = sglImage.SGLFrames[i2]; if (sglFrame.SGLFrameFormat11Or12Or22.DecodeFrame32Stream == null) { if (sglFrame.SGLFrameFormat11Or12Or22.LoadImage() == false) { break; } else { Workspace workspace = new Workspace(); workspace.Open(sglFrame.SGLFrameFormat11Or12Or22.DecodeFrame32Stream); TgaEncoder encoder = new TgaEncoder(); StringBuilder strBuilder = new StringBuilder(); strBuilder.Append(m_FolderBrowserDialog.SelectedPath); strBuilder.Append("\\["); strBuilder.Append(Path.GetFileNameWithoutExtension(m_SGLEditorDocumentForm.m_SglFileName)); strBuilder.Append("]Image["); strBuilder.Append(i.ToString("D5")); strBuilder.Append("]"); strBuilder.Append("Frame["); strBuilder.Append(i2.ToString("D3")); strBuilder.Append("]"); strBuilder.Append("Offset["); strBuilder.Append(sglFrame.CenterX.ToString("D4")); strBuilder.Append("X"); strBuilder.Append(sglFrame.CenterY.ToString("D4")); strBuilder.Append("]"); strBuilder.Append(".tga"); string imageName = strBuilder.ToString(); workspace.Save(imageName, encoder); } } } } MessageBox.Show("文件全部解压完成"); } }
public void TgaEncoder_PreserveBitsPerPixel(string imagePath, TgaBitsPerPixel bmpBitsPerPixel) { var options = new TgaEncoder(); var testFile = TestFile.Create(imagePath); using (Image <Rgba32> input = testFile.CreateRgba32Image()) { using (var memStream = new MemoryStream()) { input.Save(memStream, options); memStream.Position = 0; using (var output = Image.Load <Rgba32>(memStream)) { TgaMetadata meta = output.Metadata.GetTgaMetadata(); Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel); } } } }
private void SaveCircuitBoard(string path) { string extension = Path.GetExtension(path).ToLowerInvariant(); IImageEncoder encoder; switch (extension) { case ".png": encoder = new PngEncoder { ColorType = PngColorType.RgbWithAlpha, }; break; case ".bmp": encoder = new BmpEncoder { BitsPerPixel = BmpBitsPerPixel.Pixel32, SupportTransparency = true }; break; case ".tga": encoder = new TgaEncoder { BitsPerPixel = TgaBitsPerPixel.Pixel32 }; break; default: throw new NotSupportedException($"No image encoder available for file extension \"{extension}\"."); } using (Image <Bgra32> image = circuitBoard.ToImage().ToImageSharp()) { image.Save(path, encoder); } }
public void Encode_WithCompression_PreserveBitsPerPixel(string imagePath, TgaBitsPerPixel bmpBitsPerPixel) { var options = new TgaEncoder() { Compression = TgaCompression.RunLength }; TestFile testFile = TestFile.Create(imagePath); using (Image <Rgba32> input = testFile.CreateRgba32Image()) { using (var memStream = new MemoryStream()) { input.Save(memStream, options); memStream.Position = 0; using (Image <Rgba32> output = Image.Load <Rgba32>(memStream)) { TgaMetadata meta = output.Metadata.GetFormatMetadata(TgaFormat.Instance); Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel); } } } }
/// <summary> /// Saves the image to the given stream with the Tga format. /// </summary> /// <param name="source">The image this method extends.</param> /// <param name="stream">The stream to save the image to.</param> /// <param name="encoder">The encoder to save the image with.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public static Task SaveAsTgaAsync(this Image source, Stream stream, TgaEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( stream, encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance), cancellationToken);
/// <summary> /// Saves the image to the given stream with the Tga format. /// </summary> /// <param name="source">The image this method extends.</param> /// <param name="stream">The stream to save the image to.</param> /// <param name="encoder">The encoder to save the image with.</param> /// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public static void SaveAsTga(this Image source, Stream stream, TgaEncoder encoder) => source.Save( stream, encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance));
//returns an encoder based on image type public static ImageEncoder GetEncoderFromType(ImageType type) { ImageEncoder encoder = null; switch (type) { case ImageType.Jpeg: encoder = new JpegEncoder(); break; case ImageType.Png: encoder = new PngEncoder(); break; case ImageType.J2k: encoder = new Jp2Encoder(); break; case ImageType.Bmp: encoder = new BmpEncoder(); break; case ImageType.Emf: encoder = new EmfEncoder(); break; case ImageType.Gif: encoder = new GifEncoder(); break; case ImageType.Pcx: encoder = new PcxEncoder(); break; case ImageType.Psd: encoder = new PsdEncoder(); break; case ImageType.Tga: encoder = new TgaEncoder(); break; case ImageType.Tiff: encoder = new TiffEncoder(); break; case ImageType.Wbmp: encoder = new WbmpEncoder(); break; case ImageType.Wmf: encoder = new WmfEncoder(); break; case ImageType.Tla: encoder = new TlaEncoder(); break; default: MessageBox.Show("当前的图像格式不支持"); break; } return(encoder); }
/// <summary> /// Saves the image to the given stream with the tga format. /// </summary> /// <param name="source">The image this method extends.</param> /// <param name="path">The file path to save the image to.</param> /// <param name="encoder">The encoder to save the image with.</param> /// <exception cref="System.ArgumentNullException">Thrown if the path is null.</exception> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public static Task SaveAsTgaAsync(this Image source, string path, TgaEncoder encoder) => source.SaveAsync( path, encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance));
//returns an encoder based on image type public static ImageEncoder GetEncoderFromType( ImageType type ) { ImageEncoder encoder = null; switch ( type ) { case ImageType.Jpeg: encoder = new JpegEncoder(); break; case ImageType.Png: encoder = new PngEncoder(); break; case ImageType.J2k: encoder = new Jp2Encoder(); break; case ImageType.Bmp: encoder = new BmpEncoder(); break; case ImageType.Emf: encoder = new EmfEncoder(); break; case ImageType.Gif: encoder = new GifEncoder(); break; case ImageType.Pcx: encoder = new PcxEncoder(); break; case ImageType.Psd: encoder = new PsdEncoder(); break; case ImageType.Tga: encoder = new TgaEncoder(); break; case ImageType.Tiff: encoder = new TiffEncoder(); break; case ImageType.Wbmp: encoder = new WbmpEncoder(); break; case ImageType.Wmf: encoder = new WmfEncoder(); break; case ImageType.Tla: encoder = new TlaEncoder(); break; default: MessageBox.Show( "当前的图像格式不支持" ); break; } return encoder; }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void axPushButton_ClickEvent( object sender, EventArgs e ) { MessageBox.Show( "解压的可能时间比较长 请等待分钟 或更长的时间" ); if ( m_FolderBrowserDialog.ShowDialog() == DialogResult.OK ) { for ( int i = 0; i < m_SGLEditorDocumentForm.m_SglFileInfo.SGLImage.Length; i++ ) { SGLImage sglImage = m_SGLEditorDocumentForm.m_SglFileInfo.SGLImage[i]; for ( int i2 = 0; i2 < sglImage.SGLFrames.Length; i2++ ) { SGLFrame sglFrame = sglImage.SGLFrames[i2]; if ( sglFrame.SGLFrameFormat11Or12Or22.DecodeFrame32Stream == null ) { if ( sglFrame.SGLFrameFormat11Or12Or22.LoadImage() == false ) break; else { Workspace workspace = new Workspace(); workspace.Open( sglFrame.SGLFrameFormat11Or12Or22.DecodeFrame32Stream ); TgaEncoder encoder = new TgaEncoder(); StringBuilder strBuilder = new StringBuilder(); strBuilder.Append( m_FolderBrowserDialog.SelectedPath ); strBuilder.Append( "\\[" ); strBuilder.Append( Path.GetFileNameWithoutExtension( m_SGLEditorDocumentForm.m_SglFileName ) ); strBuilder.Append( "]Image[" ); strBuilder.Append( i.ToString( "D5" ) ); strBuilder.Append( "]" ); strBuilder.Append( "Frame[" ); strBuilder.Append( i2.ToString( "D3" ) ); strBuilder.Append( "]" ); strBuilder.Append( "Offset[" ); strBuilder.Append( sglFrame.CenterX.ToString( "D4" ) ); strBuilder.Append( "X" ); strBuilder.Append( sglFrame.CenterY.ToString( "D4" ) ); strBuilder.Append( "]" ); strBuilder.Append( ".tga" ); string imageName = strBuilder.ToString(); workspace.Save( imageName, encoder ); } } } } MessageBox.Show( "文件全部解压完成" ); } }