本文整理汇总了C#中System.Windows.Media.Imaging.PngBitmapEncoder.Save方法的典型用法代码示例。如果您正苦于以下问题:C# PngBitmapEncoder.Save方法的具体用法?C# PngBitmapEncoder.Save怎么用?C# PngBitmapEncoder.Save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.Imaging.PngBitmapEncoder
的用法示例。
在下文中一共展示了PngBitmapEncoder.Save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create3DViewPort
private void Create3DViewPort()
{
var hvp3d = new HelixViewport3D();
Viewport3D vp3d = new Viewport3D();
var lights = new DefaultLights();
var tp = new Teapot();
hvp3d.Children.Add(lights);
hvp3d.Children.Add(tp);
vp3d = hvp3d.Viewport;
tata.Children.Add(vp3d); // comenter ca pour test
/* MEGA TEST DE L'ESPACE SUBSAHARIEN */
RenderTargetBitmap bmp = new RenderTargetBitmap(800, 800, 96, 96, PixelFormats.Pbgra32);
var rect = new Rect(0, 0, 800, 800);
vp3d.Measure(new Size(800, 800));
vp3d.Arrange(rect);
vp3d.InvalidateVisual();
bmp.Render(vp3d);
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(bmp));
String filepath = "C:\\Users\\Remi\\Desktop\\canardmasque.png";
using (Stream stm = File.Create(filepath))
{
png.Save(stm);
}
}
示例2: GenerateThumbnail
/// <summary>
/// Generates an Explorer-style thumbnail for any file or shell item. Requires Vista or above.
/// </summary>
/// <param name="filename" />The filename of the item.</param>
/// <returns>The thumbnail of the item.</returns>
public static BitmapSource GenerateThumbnail(String filename, int desiredHeight)
{
var source = GenerateThumbnail(filename);
MemoryStream ms = new MemoryStream();
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(source));
encoder.Save(ms);
// perform a resize
// Do not resize as image is smaller then maxWidth and maxHeight
if (source.Height <= desiredHeight)
return source;
BitmapImage image = new BitmapImage();
image.BeginInit();
image.DecodePixelHeight = desiredHeight;
image.DecodePixelWidth = ((int)source.Width * desiredHeight) / (int)source.Height;
image.StreamSource = ms;
image.CreateOptions = BitmapCreateOptions.None;
image.CacheOption = BitmapCacheOption.Default;
image.EndInit();
return image;
}
示例3: ExportToFile
public static void ExportToFile(BitmapSource graphBitmap)
{
SaveFileDialog saveDialog = new SaveFileDialog();
const int FilterIndexJpeg = 1;
const int FilterIndexPng = 2;
saveDialog.Filter = "JPEG|*.jpg|PNG|*.png";
saveDialog.Title = "Save Graph As";
saveDialog.AddExtension = true;
saveDialog.ShowDialog();
if (string.IsNullOrEmpty(saveDialog.FileName))
{
return;
}
using (FileStream fileStream = (FileStream)saveDialog.OpenFile())
{
BitmapEncoder bitmapEncoder;
switch (saveDialog.FilterIndex)
{
case FilterIndexJpeg:
bitmapEncoder = new JpegBitmapEncoder();
break;
case FilterIndexPng:
bitmapEncoder = new PngBitmapEncoder();
break;
default:
throw new ArgumentException("Invalid file save type");
}
bitmapEncoder.Frames.Add(BitmapFrame.Create(graphBitmap));
bitmapEncoder.Save(fileStream);
}
}
示例4: SavePatient
private void SavePatient()
{
// string paths = @"c:\amHealth\images";
string filePath = @"c:\amHealth\\images\" +fname.Text+"-"+lname.Text +".jpg";
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)imgCapture.Source));
using (FileStream stream = new FileStream(filePath, FileMode.Create))
encoder.Save(stream);
_patient = App.amApp.Patients.Add();
_patient.Lname = lname.Text;
_patient.Fname = fname.Text;
_patient.Gender = gender.Text;
_patient.Dob = dob.Text;
_patient.Height = height.Text;
_patient.Weight = weight.Text;
_patient.Phone = phone.Text;
_patient.Region = region.Text;
_patient.Sync = "F";
_patient.Email = email.Text;
_patient.Org = "test";
_patient.Image = fname.Text + "-" + lname.Text + ".jpg";
_patient.Save();
System.Windows.MessageBox.Show("Patient saved ");
this.DialogResult = true;
}
示例5: GetColorFromImage
private Color GetColorFromImage(Point p)
{
try
{
var bounds = VisualTreeHelper.GetDescendantBounds(this);
var rtb = new RenderTargetBitmap((int) bounds.Width, (int) bounds.Height, 96, 96, PixelFormats.Default);
rtb.Render(this);
byte[] arr;
var png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (var stream = new MemoryStream())
{
png.Save(stream);
arr = stream.ToArray();
}
BitmapSource bitmap = BitmapFrame.Create(new MemoryStream(arr));
var pixels = new byte[4];
var cb = new CroppedBitmap(bitmap, new Int32Rect((int) p.X, (int) p.Y, 1, 1));
cb.CopyPixels(pixels, 4, 0);
return Color.FromArgb(pixels[3], pixels[2], pixels[1], pixels[0]);
}
catch (Exception)
{
return ColorBox.Color;
}
}
示例6: ButtonScreenshotClick
/// <summary>
/// Handles the user clicking on the screenshot button
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void ButtonScreenshotClick(object sender, RoutedEventArgs e)
{
if (null == this.sensor)
{
this.statusBarText.Text = Properties.Resources.ConnectDeviceFirst;
return;
}
// create a png bitmap encoder which knows how to save a .png file
BitmapEncoder encoder = new PngBitmapEncoder();
// create frame from the writable bitmap and add to encoder
encoder.Frames.Add(BitmapFrame.Create(this.colorBitmap));
string time = System.DateTime.Now.ToString("hh'-'mm'-'ss", CultureInfo.CurrentUICulture.DateTimeFormat);
string myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
string path = Path.Combine(myPhotos, "KinectSnapshot-" + time + ".png");
// write the new file to disk
try
{
using (FileStream fs = new FileStream(path, FileMode.Create))
{
encoder.Save(fs);
}
this.statusBarText.Text = string.Format(CultureInfo.InvariantCulture, "{0} {1}", Properties.Resources.ScreenshotWriteSuccess, path);
}
catch (IOException)
{
this.statusBarText.Text = string.Format(CultureInfo.InvariantCulture, "{0} {1}", Properties.Resources.ScreenshotWriteFailed, path);
}
}
示例7: RenderPages
/// <summary>
/// Generates an image of each page in the year book
/// and saves it to the src folder
/// </summary>
/// <param name="bv"></param>
/// <param name="folderloc"></param>
private static void RenderPages(BookViewer bv, string folderloc)
{
int currentpage = bv.ViewIndex;
//loops though each page
foreach (Page p in bv.CurrentBook.Pages)
{
bv.ViewIndex = p.PageNumber;
//forces the canvas to re-render
BookViewer.DesignerCanvas.UpdateLayout();
//takes a picture of the canvas
RenderTargetBitmap rtb = new RenderTargetBitmap(PaperSize.Pixel.PaperWidth, PaperSize.Pixel.PaperHeight, 96, 96, PixelFormats.Default);
rtb.Render(BookViewer.DesignerCanvas);
//getting the bleed margin
Int32Rect bleedmargin = new Int32Rect((PaperSize.Pixel.PaperWidth - PaperSize.Pixel.BleedWidth) / 2, (PaperSize.Pixel.PaperHeight - PaperSize.Pixel.BleedHeight) / 2, PaperSize.Pixel.BleedWidth, PaperSize.Pixel.BleedHeight);
//cropping the image
CroppedBitmap cb = new CroppedBitmap(rtb, bleedmargin);
//encodes the image in png format
PngBitmapEncoder pbe = new PngBitmapEncoder();
pbe.Frames.Add(BitmapFrame.Create(cb));
//saves the resulting image
FileStream fs = File.Open(folderloc + "\\src\\" + (p.PageNumber+1) + ".png", FileMode.Create);
pbe.Save(fs);
fs.Flush();
fs.Close();
}
bv.ViewIndex = currentpage;
}
示例8: snapshotClicked
private void snapshotClicked(object sender, RoutedEventArgs e)
{
e.Handled = true;
var imageSource = GridZoomControl.SnapshotToBitmapSource();
if (imageSource == null)
{
MessageBox.Show("Failed to snapshot image");
return;
}
var dialog = new SaveFileDialog
{
FileName = "Snapshot.png", Filter = "PNG File (*.png)|*.png"
};
if (dialog.ShowDialog(this) == true)
{
using (var fs = new FileStream(dialog.FileName, FileMode.Create))
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(imageSource));
encoder.Save(fs);
}
}
}
示例9: ExportChartPlotterPng
/// <summary>
/// ExportChartPlotterPng
/// </summary>
/// <param name="path"></param>
/// <param name="surface"></param>
public static void ExportChartPlotterPng(Uri path, ChartPlotter surface)
{
if (path == null) return;
//Save current canvas transform 保存当前画布变换
Transform transform = surface.LayoutTransform;
//reset current transform (in case it is scaled or rotated) 重设当前画布(如果缩放或旋转)
surface.LayoutTransform = null;
//Create a render bitmap and push the surface to it 创建一个渲染位图和表面
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
(int)surface.Width,
(int)surface.Height,
96d, 96d,
PixelFormats.Pbgra32);
renderBitmap.Render(surface);
// Create a file stream for saving image
using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
{
//Use png encoder for our data
PngBitmapEncoder encoder = new PngBitmapEncoder();
// push the rendered bitmap to it
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// save the data to the stream
encoder.Save(outStream);
}
// Restore previously saved layout 恢复以前保存布局
surface.LayoutTransform = transform;
}
示例10: RenderToFile
public void RenderToFile(UserControl target, [CallerMemberName] string testName = "")
{
string path = Path.Combine(testDirectory, testName + ".wpf.out.png");
RenderTargetBitmap bitmap = new RenderTargetBitmap(
(int)target.Width,
(int)target.Height,
96,
96,
PixelFormats.Pbgra32);
Size size = new Size(target.Width, target.Height);
target.Measure(size);
target.Arrange(new Rect(size));
bitmap.Render(target);
File.Delete(path);
using (FileStream fs = new FileStream(path, FileMode.Create))
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.Save(fs);
}
}
示例11: PngBitmap
/// <summary>
/// Returns the contents of a WPF Visual as a Bitmap in PNG format.
/// </summary>
/// <param name="visual">A WPF Visual.</param>
/// <returns>A GDI+ System.Drawing.Bitmap.</returns>
public static Bitmap PngBitmap(this Visual visual)
{
// Get height and width
int height = (int)(double)visual.GetValue(FrameworkElement.ActualWidthProperty);
int width = (int)(double)visual.GetValue(FrameworkElement.ActualHeightProperty);
// Render
RenderTargetBitmap rtb =
new RenderTargetBitmap(
height,
width,
96,
96,
PixelFormats.Default);
rtb.Render(visual);
// Encode
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
System.IO.MemoryStream stream = new System.IO.MemoryStream();
encoder.Save(stream);
// Create Bitmap
Bitmap bmp = new Bitmap(stream);
stream.Close();
return bmp;
}
示例12: Convert
public BitmapImage Convert(BitmapSource bitmap)
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
var memoryStream = new MemoryStream();
encoder.Save(memoryStream);
var bitmapImage = new BitmapImage();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.CreateOptions = BitmapCreateOptions.None;
try
{
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());
}
finally
{
bitmapImage.EndInit();
bitmapImage.Freeze();
memoryStream.Close();
}
return bitmapImage;
}
示例13: Scale
public static Bitmap Scale(RenderTargetBitmap bitmap, float ScaleFactorX, float ScaleFactorY)
{
int scaleWidth = (int)Math.Max(bitmap.Width * ScaleFactorX, 1.0f);
int scaleHeight = (int)Math.Max(bitmap.Height * ScaleFactorY, 1.0f);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
var stream = new MemoryStream();
encoder.Save(stream);
var frombitmap = new Bitmap(stream);
stream.Close();
stream.Dispose();
var newbitmap = (Bitmap)frombitmap;
Bitmap scaledBitmap = new Bitmap(scaleWidth, scaleHeight);
using (Graphics gr = Graphics.FromImage(scaledBitmap))
{
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gr.DrawImage(newbitmap, new System.Drawing.Rectangle(0, 0, scaleWidth, scaleHeight), new System.Drawing.Rectangle(0, 0, (int)bitmap.Width, (int)bitmap.Height), GraphicsUnit.Pixel);
}
return scaledBitmap;
}
示例14: SaveAsPNG
public static void SaveAsPNG(this PlotView plot)
{
if (plot == null) return;
string path;
SaveFileDialog file = new SaveFileDialog();
file.Filter = @"png files (*.png)|*.png";
file.RestoreDirectory = true;
if (file.ShowDialog() == DialogResult.OK)
{
path = file.FileName;
}
else
{
return;
}
BitmapSource bmp = plot.ToBitmap();
using (FileStream stream = new FileStream(path, FileMode.Create))
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(stream);
}
}
示例15: SaveFamily
private void SaveFamily()
{
// string paths = @"c:\amHealth\images";
string filePath = @"c:\amHealth\\images\" + name.Text + ".jpg";
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)imgCapture.Source));
using (FileStream stream = new FileStream(filePath, FileMode.Create))
encoder.Save(stream);
_family = App.amApp.Familys.Add();
_family.Name = name.Text;
_family.Phone = phone.Text;
_family.Relationship = relationship.Text;
_family.Sync = "F";
if (chkNotify.IsChecked == true)
{
_family.Notify = "true";
}
else
{
_family.Notify = "false";
}
_family.Email = email.Text;
_family.Patient = patient;
_family.Image = name.Text + ".jpg";
_family.Save();
System.Windows.MessageBox.Show("Family saved ");
this.DialogResult = true;
}