本文整理汇总了C#中System.Windows.Media.Imaging.JpegBitmapEncoder类的典型用法代码示例。如果您正苦于以下问题:C# JpegBitmapEncoder类的具体用法?C# JpegBitmapEncoder怎么用?C# JpegBitmapEncoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JpegBitmapEncoder类属于System.Windows.Media.Imaging命名空间,在下文中一共展示了JpegBitmapEncoder类的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: ToImageFile
public static void ToImageFile(this Visual theVisual, string fileName)
{
BitmapEncoder encoder;
switch (Path.GetExtension(fileName).ToLower())
{
case ".jpg":
case ".jpeg":
encoder = new JpegBitmapEncoder();
break;
case ".png":
encoder = new PngBitmapEncoder();
break;
case ".bmp":
encoder = new BmpBitmapEncoder();
break;
case ".gif":
encoder = new GifBitmapEncoder();
break;
case ".tiff":
encoder = new TiffBitmapEncoder();
break;
default:
throw new EncoderFallbackException("The Specified Filename is not a known image type. Supported image formats are jpeg, png, bmp, gif, and tiff.");
}
encoder.Frames.Add(BitmapFrame.Create(theVisual.ToBitmapSource()));
using (var stream = new FileStream(fileName, FileMode.Create)) encoder.Save(stream);
}
示例3: SaveAsImage
/// <summary>
/// Saves the full chart to a file, including technical analysis.
/// </summary>
/// <param name="filename">The file to write to.</param>
public void SaveAsImage(string filename)
{
if (string.IsNullOrEmpty(filename))
throw new ArgumentNullException();
var extension = Path.GetExtension(filename);
if (string.IsNullOrEmpty(extension))
throw new ArgumentNullException();
BitmapEncoder bitmapEncoder;
extension = extension.ToLower();
if (extension == ".png")
bitmapEncoder = new PngBitmapEncoder();
else if (extension == ".jpg" || extension == ".jpeg")
bitmapEncoder = new JpegBitmapEncoder();
else if (extension == ".gif")
bitmapEncoder = new GifBitmapEncoder();
else if (extension == ".bmp")
bitmapEncoder = new BmpBitmapEncoder();
else throw new ArgumentException("Cannot find a BitmapEncoder for this file type.");
var renderTargetBitmap = new RenderTargetBitmap((int)this.ActualWidth, (int)this.ActualHeight, 96, 96, PixelFormats.Pbgra32);
foreach (var child in LogicalTreeHelper.GetChildren(this))
{
if (child is ChartPanel)
renderTargetBitmap.Render((ChartPanel)child);
}
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
using (Stream stream = File.Create(filename))
{
bitmapEncoder.Save(stream);
}
}
示例4: AddMedium
public int AddMedium(string name, object obj)
{
var media = db.CreateObject<medium>();
if (name == "Image")
{
byte[] img;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(obj as BitmapSource));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
img = ms.ToArray();
}
media.media_type_id = 1;
[email protected] = img;
}
else if (name == "Movie")
{
media.media_type_id = 2;
[email protected] = System.Text.Encoding.UTF8.GetBytes(obj as string);
}
else
{
media.media_type_id = 3;
//BinaryFormatter bf = new BinaryFormatter();
//MemoryStream ms = new MemoryStream();
//bf.Serialize(ms, obj);
//[email protected] = ms.ToArray();
[email protected] = System.Text.Encoding.UTF8.GetBytes(obj as string);
}
db.media.AddObject(media);
db.SaveChanges();
return (int)media.id;
}
示例5: Export
/// <summary>
/// Exports the specified viewport.
/// </summary>
/// <param name="viewport">
/// The viewport.
/// </param>
public void Export(Viewport3D viewport)
{
int m = this.OversamplingMultiplier;
var background = this.Background;
if (background == null)
{
background = Brushes.Transparent;
}
var bmp = Viewport3DHelper.RenderBitmap(viewport, background, m);
BitmapEncoder encoder;
string ext = Path.GetExtension(this.FileName);
switch (ext.ToLower())
{
case ".jpg":
var jpg = new JpegBitmapEncoder();
jpg.Frames.Add(BitmapFrame.Create(bmp));
encoder = jpg;
break;
case ".png":
var png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(bmp));
encoder = png;
break;
default:
throw new InvalidOperationException("Not supported file format.");
}
using (Stream stm = File.Create(this.FileName))
{
encoder.Save(stm);
}
}
示例6: SaveAsImage
public static void SaveAsImage(FrameworkElement visual)
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.DefaultExt = ".png";
dlg.Filter = "PNG Image (.png)|*.png|JPEG Image (.jpg)|*.jpg";
if (dlg.ShowDialog().Value)
{
BitmapSource img = (BitmapSource)ToImageSource(visual);
FileStream stream = new FileStream(dlg.FileName, FileMode.Create);
BitmapEncoder encoder = null; // new BitmapEncoder();
if (dlg.SafeFileName.ToLower().EndsWith(".png"))
{
encoder = new PngBitmapEncoder();
}
else
{
encoder = new JpegBitmapEncoder();
}
encoder.Frames.Add(BitmapFrame.Create(img));
encoder.Save(stream);
stream.Close();
}
}
示例7: Save
public static void Save(this BitmapSource image, string filePath, ImageFormat format)
{
BitmapEncoder encoder = null;
switch(format)
{
case ImageFormat.Png:
encoder = new PngBitmapEncoder();
break;
case ImageFormat.Jpeg:
encoder = new JpegBitmapEncoder();
break;
case ImageFormat.Bmp:
encoder = new BmpBitmapEncoder();
break;
}
if (encoder == null)
return;
encoder.Frames.Add(BitmapFrame.Create(BitmapFrame.Create(image)));
using (var stream = new FileStream(filePath, FileMode.Create))
encoder.Save(stream);
}
示例8: OnSaveImageClick
// Handles save image click
private void OnSaveImageClick(object sender, RoutedEventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "24-bit Bitmap (*.bmp)|*.bmp|JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|GIF (*.gif)|*.gif|PNG (*.png)|*.png";
dlg.FilterIndex = 4;
bool? dlgResult = dlg.ShowDialog(Window.GetWindow(this));
if(dlgResult.HasValue && dlgResult.Value)
{
Visualizer visualizer = Visualizer;
RenderTargetBitmap bitmap = new RenderTargetBitmap((int)previewPage.PageWidth, (int)previewPage.PageHeight, 96, 96, PixelFormats.Pbgra32);
visualizer.RenderTo(bitmap);
BitmapEncoder encoder = null;
string ext = System.IO.Path.GetExtension(dlg.FileName);
if (ext == "bmp") encoder = new BmpBitmapEncoder();
else if ((ext == "jpg") || (ext == "jpeg")) encoder = new JpegBitmapEncoder();
else encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
using (FileStream stream = new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write))
{
encoder.Save(stream);
stream.Flush();
}
}
}
示例9: Bitmap2BitmapImage
public static BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitmap();
BitmapImage res =new BitmapImage();
try
{
BitmapSource retval = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
MemoryStream memoryStream = new MemoryStream();
encoder.Frames.Add(BitmapFrame.Create(retval));
encoder.Save(memoryStream);
res.BeginInit();
res.StreamSource = new MemoryStream(memoryStream.ToArray());
res.EndInit();
}
finally
{
DeleteObject(hBitmap);
}
return res;
}
示例10: Save
public void Save(BitmapSource bmSource)
{
BitmapEncoder encoder;
switch (Path.GetExtension(_path).ToLower())
{
case ".jpg":
case ".jpeg":
encoder = new JpegBitmapEncoder();
break;
case ".gif":
encoder = new GifBitmapEncoder();
break;
case ".bmp":
encoder = new BmpBitmapEncoder();
break;
case ".tif":
case ".tiff":
encoder = new TiffBitmapEncoder();
break;
case ".png":
encoder = new PngBitmapEncoder();
break;
default:
throw new ArgumentException("Wrong path");
}
encoder.Frames.Add(BitmapFrame.Create(bmSource));
Stream stm = File.Create(_path);
encoder.Save(stm);
stm.Dispose();
}
示例11: CustomeColorizer
public void CustomeColorizer(string saveLocation, List<Colorizer> colorpalette)
{
if (_data == null)
throw new InvalidDataException("You must Solve the case First!");
if (colorpalette.Count <= 1)
throw new InvalidDataException("You must choose at least 2 Colors");
PixelFormat pf = PixelFormats.Rgb24;
const double dpi = 96;
int rawStride = (_width*pf.BitsPerPixel + 7)/8;
var pixelData = new byte[rawStride*_height];
foreach (Colorizer colorizer in colorpalette)
{
for (int i = 0; i < _data.Length; i++)
{
if (Helper.IsInBetween(_data[i], colorizer.Min, colorizer.Max))
{
SetPixel((i%_width), (i/_width), colorizer.Color, ref pixelData, rawStride);
}
}
}
BitmapSource bitmap = BitmapSource.Create(_width, _height, dpi, dpi, pf, null, pixelData, rawStride);
var stream = new FileStream(saveLocation, FileMode.Create);
var encoder = new JpegBitmapEncoder {QualityLevel = 100};
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.Save(stream);
stream.Close();
}
示例12: GetBitmapEncoder
private static BitmapEncoder GetBitmapEncoder(ImageContentType imageContentType)
{
BitmapEncoder bitmapEncoder;
switch (imageContentType)
{
case ImageContentType.ImageBmpContentType:
bitmapEncoder = new BmpBitmapEncoder();
break;
case ImageContentType.ImageGifContentType:
bitmapEncoder = new GifBitmapEncoder();
break;
case ImageContentType.ImageJpegContentType:
bitmapEncoder = new JpegBitmapEncoder();
break;
case ImageContentType.ImageTiffContentType:
bitmapEncoder = new TiffBitmapEncoder();
break;
case ImageContentType.ImagePngContentType:
bitmapEncoder = new PngBitmapEncoder();
break;
default:
bitmapEncoder = new PngBitmapEncoder();
break;
}
return bitmapEncoder;
}
示例13: 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;
}
示例14: 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);
}
}
示例15: 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;
}