本文整理汇总了C#中System.Windows.Media.Imaging.JpegBitmapEncoder.Save方法的典型用法代码示例。如果您正苦于以下问题:C# JpegBitmapEncoder.Save方法的具体用法?C# JpegBitmapEncoder.Save怎么用?C# JpegBitmapEncoder.Save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.Imaging.JpegBitmapEncoder
的用法示例。
在下文中一共展示了JpegBitmapEncoder.Save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Clicked
private void Clicked(object sender, RoutedEventArgs e)
{
var result = Clipboard.GetImage();
if (result == null)
{
MessageBox.Show("No image");
return;
}
var directorypath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Screendumps");
var filePath = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".jpg";
var directory = new DirectoryInfo(directorypath);
if (!directory.Exists)
{
directory.Create();
}
using (var stream = new FileStream(Path.Combine(directorypath,filePath), FileMode.Create))
{
var encode = new JpegBitmapEncoder();
encode.QualityLevel = 100;
encode.Frames.Add(BitmapFrame.Create(result));
encode.Save(stream);
}
Clipboard.SetText(Path.Combine(directorypath,filePath));
}
示例2: SetWallPaper
/// <summary>
/// Sets wallpaper and style
/// WinowsAPI Call
/// </summary>
/// <param name="image"></param>
/// <param name="style"></param>
public static void SetWallPaper(BitmapImage image, Style style)
{
if (image != null)
{
string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper");
//image.Save(tempPath, System.Drawing.Imaging.ImageFormat.Jpeg)
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
try
{
encoder.Frames.Add(BitmapFrame.Create(image));
}
catch { }
using(Stream fs = new FileStream(tempPath, FileMode.Create))
{
encoder.Save(fs);
}
SetWallPaperStyle(style);
//Win API Call
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, tempPath, SPIF_UPDATEINIFILE | SPIF_SENDWINIINICHANGE);
}
}
示例3: btnCapture_Click
private void btnCapture_Click(object sender, RoutedEventArgs e)
{
//captureElement. 怎么抓取高清的原始图像
//能不能抓视频。
//todo:怎么只抓取一部分
RenderTargetBitmap bmp = new RenderTargetBitmap(
(int)captureElement.ActualWidth,
(int)captureElement.ActualHeight,
96, 96, PixelFormats.Default);
//为避免抓不全的情况,需要在Render之前调用Measure、Arrange
//为避免VideoCaptureElement显示不全,需要把
//VideoCaptureElement的Stretch="Fill"
captureElement.Measure(captureElement.RenderSize);
captureElement.Arrange(new Rect(captureElement.RenderSize));
bmp.Render(captureElement);
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
CaptureData = ms.ToArray();
}
//captureElement.Pause();
//todo:自己完成重拍的代码
DialogResult = true;
}
示例4: SaveFile
//Метод, который отвечает сохранение изображения
public void SaveFile(object sender, RoutedEventArgs e)
{
SaveFileDialog sfdialog = new SaveFileDialog();
sfdialog.Filter = "JPEG image|*.jpg;*.jpeg";
sfdialog.FileName = "default";
sfdialog.DefaultExt = ".jpg";
bool? result = sfdialog.ShowDialog();
if (result == true)
{
string fileName = sfdialog.FileName;
int width = (int)surface.ActualWidth;
int height = (int)surface.ActualHeight;
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
renderBitmap.Render(surface);
using(FileStream stream = new FileStream(fileName,FileMode.Create))
{
JpegBitmapEncoder jpeg = new JpegBitmapEncoder();
jpeg.Frames.Add(BitmapFrame.Create(renderBitmap));
jpeg.Save(stream);
}
}
}
示例5: GetData
public override void GetData(object target, Stream outgoingData)
{
var source = (BitmapSource)target;
if (source == null)
throw new NullReferenceException("Make sure your BitmapSource is not null.");
var encoder = new JpegBitmapEncoder { QualityLevel = 80 };
try
{
encoder.Frames.Add(BitmapFrame.Create(source));
}
catch (Exception)
{
throw new Exception("Make sure your BitmapSource is correctly initialized.");
}
byte[] outgoingBytes;
using (var stream = new MemoryStream())
{
encoder.Save(stream);
outgoingBytes = stream.ToArray();
stream.Close();
}
outgoingData.Write(outgoingBytes, 0, outgoingBytes.Length);
}
示例6: btnSave_Click
private void btnSave_Click(object sender, RoutedEventArgs e)
{
Predicate<BitmapImage> match = null;
try
{
if (!Directory.Exists(Path.Combine(UserSettings.ScreensDir, this.g.FileSafeTitle)))
{
Directory.CreateDirectory(Path.Combine(UserSettings.ScreensDir, this.g.FileSafeTitle));
}
if (match == null)
{
match = i => i == ((BitmapImage) this.imgBig.Source);
}
int num = Array.FindIndex<BitmapImage>(this._imageLinks.ToArray<BitmapImage>(), match);
if (num == -1)
{
throw new Exception("index out of bounds");
}
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
string path = Path.Combine(Path.Combine(UserSettings.ScreensDir, this.g.FileSafeTitle), string.Concat(new object[] { this.g.FileSafeTitle, "-", num, ".jpg" }));
encoder.Frames.Add(BitmapFrame.Create((BitmapImage) this.imgBig.Source));
using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
encoder.Save(stream);
}
Toaster.Instance.ShowToast("Successfully saved screenshot to " + path, 0x5dc);
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
Toaster.Instance.ShowToast("Error saving screenshot", 0x5dc);
}
}
示例7: adicionaMetadadosNaImagem
private static void adicionaMetadadosNaImagem(ItemDePauta item)
{
String caminhoArquivoOrigem = Path.Combine(ConfigurationManager.AppSettings["caminhoEntrada"], item.nomeArquivoImagem);
String caminhoSaida = Path.Combine(ConfigurationManager.AppSettings["caminhoSaida"],
item.nomeImagemNaPauta + "_" + item.nomeArquivoImagem);
using (Stream file = File.Open(caminhoArquivoOrigem, FileMode.Open, FileAccess.ReadWrite)) {
BitmapDecoder decoder = BitmapDecoder.Create(file, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
if (decoder.Frames[0] != null && decoder.Frames[0].Metadata != null) {
BitmapFrame frameCopy = (BitmapFrame)decoder.Frames[0].Clone();
BitmapMetadata metadata = decoder.Frames[0].Metadata.Clone() as BitmapMetadata;
try {
metadata.SetQuery("/xmp/dc:description/x-default", item.descricao);
} catch (Exception) {
metadata.SetQuery("/xmp/dc:description", item.descricao);
}
metadata.SetQuery("System.Keywords", item.palavrasChave);
metadata.Copyright = item.copyright;
encoder.Frames.Add(BitmapFrame.Create(frameCopy, frameCopy.Thumbnail, metadata, frameCopy.ColorContexts));
using (Stream outputFile = File.Open(caminhoSaida, FileMode.Create, FileAccess.ReadWrite)) {
encoder.Save(outputFile);
}
}
}
}
示例8: RecordedVideoFrame
public RecordedVideoFrame(ColorImageFrame colorFrame)
{
if (colorFrame != null)
{
byte[] bits = new byte[colorFrame.PixelDataLength];
colorFrame.CopyPixelDataTo(bits);
int BytesPerPixel = colorFrame.BytesPerPixel;
int Width = colorFrame.Width;
int Height = colorFrame.Height;
var bmp = new WriteableBitmap(Width, Height, 96, 96, PixelFormats.Bgr32, null);
bmp.WritePixels(new System.Windows.Int32Rect(0, 0, Width, Height), bits, Width * BytesPerPixel, 0);
JpegBitmapEncoder jpeg = new JpegBitmapEncoder();
jpeg.Frames.Add(BitmapFrame.Create(bmp));
var SaveStream = new MemoryStream();
jpeg.Save(SaveStream);
SaveStream.Flush();
JpegData = SaveStream.ToArray();
}
else
{
return;
}
}
示例9: ConvertBack
/// <summary>
/// Converts ImageSource to byte[]
/// </summary>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
/// <param name="value">The value that is produced by the binding target.</param><param name="targetType">The type to convert to.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var renderTargetBitmap = value as RenderTargetBitmap;
if (null != renderTargetBitmap)
{
var bitmapImage = new BitmapImage();
var bitmapEncoder = new BmpBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
using (var stream = new MemoryStream())
{
bitmapEncoder.Save(stream);
stream.Seek(0, SeekOrigin.Begin);
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
}
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
byte[] data = null;
using (var ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
return data;
}
return null;
}
示例10: Button1_Click
public void Button1_Click(object sender, RoutedEventArgs e)
{
try
{
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@text1.Text);
myBitmapImage.EndInit();
FormatConvertedBitmap gray = new FormatConvertedBitmap(myBitmapImage, PixelFormats.Gray4, null, 0);
myImage.Source = gray;
var enc = new JpegBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(gray));
using (var s = File.Create("gray.jpg"))
enc.Save(s);
}
catch
{
text1.Text = "Не верно введен путь";
}
/* public void Button2_Click(object sender, RoutedEventArgs e)
{
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource=new Uri(myImage);
myBitmapImage.EndInit();
var enc = new JpegBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(gray));
using (var s = File.Create("gray.jpg"))
enc.Save(s);*/
}
示例11: SaveUserImage
/// <summary>
/// Store into the compiled folder the user photo used to retrieve user information
/// </summary>
/// <param name="faceImageSource">face image source</param>
/// <param name="imageFileName">face image name</param>
/// <returns>1 if store success, 0 otherwise</returns>
public static int SaveUserImage(BitmapSource faceImageSource, string imageFileName)
{
int resultCode = 1;
if (File.Exists(imageFileName))
{
File.Delete(imageFileName);
}
using (FileStream saveSnapshotStream = new FileStream(imageFileName, FileMode.CreateNew))
{
try
{
JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
jpgEncoder.QualityLevel = 100;
jpgEncoder.Frames.Add(BitmapFrame.Create(faceImageSource));
jpgEncoder.Save(saveSnapshotStream);
saveSnapshotStream.Flush();
saveSnapshotStream.Close();
saveSnapshotStream.Dispose();
}
catch (Exception e)
{
resultCode = 0;
}
}
return resultCode;
}
示例12: GetScreenShot
public static byte[] GetScreenShot(this UIElement source, double scale, int quality)
{
double renderHeight = source.RenderSize.Height * scale;
double renderWidth = source.RenderSize.Width * scale;
RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
VisualBrush sourceBrush = new VisualBrush(source);
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
using (drawingContext)
{
drawingContext.PushTransform(new ScaleTransform(scale, scale));
drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(source.RenderSize.Width, source.RenderSize.Height)));
}
renderTarget.Render(drawingVisual);
JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
jpgEncoder.QualityLevel = quality;
jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
Byte[] image;
using (MemoryStream outputStream = new MemoryStream())
{
jpgEncoder.Save(outputStream);
image = outputStream.ToArray();
}
return image;
}
示例13: GetJpgImage
/// <summary>
/// </summary>
/// <param name="source"> </param>
/// <param name="scale"> </param>
/// <param name="quality"> </param>
/// <returns> </returns>
public static byte[] GetJpgImage(UIElement source, double scale, int quality)
{
var actualHeight = source.RenderSize.Height;
var actualWidth = source.RenderSize.Width;
var renderHeight = actualHeight * scale;
var renderWidth = actualWidth * scale;
var renderTarget = new RenderTargetBitmap((int) renderWidth, (int) renderHeight, 96, 96, PixelFormats.Pbgra32);
var sourceBrush = new VisualBrush(source);
var drawingVisual = new DrawingVisual();
var drawingContext = drawingVisual.RenderOpen();
using (drawingContext)
{
drawingContext.PushTransform(new ScaleTransform(scale, scale));
drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight)));
}
renderTarget.Render(drawingVisual);
var jpgEncoder = new JpegBitmapEncoder
{
QualityLevel = quality
};
jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
Byte[] imageArray;
using (var outputStream = new MemoryStream())
{
jpgEncoder.Save(outputStream);
imageArray = outputStream.ToArray();
}
return imageArray;
}
示例14: ConvertImageSourceToByteArray
/// <summary>
/// Converts an <see cref="ImageSource"/> to an array of bytes.
/// </summary>
/// <param name="image"><see creConvertImageSourceToByteArrayf="ImageSource"/> to convert.</param>
/// <returns>Array of bytes.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="image"/> is <c>null</c>.</exception>
public static byte[] ConvertImageSourceToByteArray(this ImageSource image)
{
Argument.IsNotNull("image", image);
byte[] result;
BitmapFrame bitmapFrame = null;
var encoder = new JpegBitmapEncoder();
var bitmapSource = (BitmapSource)image;
bitmapFrame = BitmapFrame.Create(bitmapSource);
encoder.Frames.Add(bitmapFrame);
using (var memoryStream = new MemoryStream())
{
encoder.Save(memoryStream);
result = memoryStream.ToArray();
encoder.Frames.Clear();
}
// Force garbage collection to prevent lots of memory usage
GC.WaitForPendingFinalizers();
GC.Collect();
return result;
}
示例15: SaveImageCapture
public static void SaveImageCapture(BitmapSource bitmap)
{
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.QualityLevel = 100;
// Configure save file dialog box
var dlg = new Microsoft.Win32.SaveFileDialog
{
FileName = "Image",
DefaultExt = ".Jpg",
Filter = "Image (.jpg)|*.jpg"
};
// Show save file dialog box
var result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
{
// Save Image
string filename = dlg.FileName;
var fstream = new FileStream(filename, FileMode.Create);
encoder.Save(fstream);
fstream.Close();
}
}