本文整理汇总了C#中System.Drawing.Image.Save方法的典型用法代码示例。如果您正苦于以下问题:C# Image.Save方法的具体用法?C# Image.Save怎么用?C# Image.Save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Image
的用法示例。
在下文中一共展示了Image.Save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ImageToBytes
/// <summary>
/// Convert Image to Byte[]
/// Image img = Image.FromFile("c:\\hcha.jpg");
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static byte[] ImageToBytes(Image image)
{
ImageFormat format = image.RawFormat;
using (MemoryStream ms = new MemoryStream())
{
if (format.Equals(ImageFormat.Jpeg))
{
image.Save(ms, ImageFormat.Jpeg);
}
else if (format.Equals(ImageFormat.Png))
{
image.Save(ms, ImageFormat.Png);
}
else if (format.Equals(ImageFormat.Bmp))
{
image.Save(ms, ImageFormat.Bmp);
}
else if (format.Equals(ImageFormat.Gif))
{
image.Save(ms, ImageFormat.Gif);
}
else if (format.Equals(ImageFormat.Icon))
{
image.Save(ms, ImageFormat.Icon);
}
byte[] buffer = new byte[ms.Length];
//Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
ms.Seek(0, SeekOrigin.Begin);
ms.Read(buffer, 0, buffer.Length);
return buffer;
}
}
示例2: FromImageV4
public static string FromImageV4(Image val)
{
MemoryStream stream = new MemoryStream();
try
{
val.Save(stream, val.RawFormat);
}
catch (ArgumentNullException)
{
val.Save(stream, ImageFormat.Bmp);
}
return FromStreamV4(stream);
}
示例3: FromImage
public static string FromImage(Image val)
{
MemoryStream stream = new MemoryStream();
try
{
val.Save(stream, val.RawFormat);
}
catch (ArgumentNullException)
{
val.Save(stream, ImageFormat.Bmp);
}
return Convert.ToBase64String(stream.GetBuffer());
}
示例4: SaveImage
private static void SaveImage(Image image, Stream stream)
{
if (Codecs.Contains(image.RawFormat.Guid))
image.Save(stream, image.RawFormat);
else
image.Save(stream, ImageFormat.Png);
}
示例5: AddOrUpdateMapTile
public bool AddOrUpdateMapTile(SceneInfo sceneInfo, Image mapTile)
{
if (m_assetClient == null)
return false;
int zoomLevel = 1;
uint x = (uint)sceneInfo.MinPosition.X / 256u;
uint y = (uint)sceneInfo.MinPosition.Y / 256u;
byte[] pngData;
using (MemoryStream stream = new MemoryStream())
{
mapTile.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
pngData = stream.ToArray();
}
Asset asset = new Asset
{
ContentType = "image/png",
CreationDate = DateTime.UtcNow,
CreatorID = sceneInfo.ID,
Data = pngData,
ID = TileNameToUUID(zoomLevel, x, y)
};
// TODO: Create and store the other zoom levels
return m_assetClient.StoreAsset(asset);
}
示例6: GetCaptcha
public string GetCaptcha(Image image)
{
bool flag = true;
int retries = 5;
string id = "";
MemoryStream ms = new MemoryStream();
image.Save(ms,ImageFormat.Jpeg);
string data = Convert.ToBase64String(ms.ToArray());
data = WebUtility.UrlEncode(data);
data = String.Format(inData, token, data);
while (flag && retries > 0)
{
flag = false;
WebClient web = new WebClient();
web.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
try
{
string res = web.UploadString(inUrl, data);
if (res.Contains("OK|"))
id = res.Substring(3);
else throw new CaptchaErrorException();
}
catch (Exception)
{
retries--;
flag = true;
}
}
if (flag)
return null;
return GetResponse(id);
}
示例7: getBytesFromImage
// Convertit une image en bytes
public static byte[] getBytesFromImage(Image im)
{
MemoryStream ms = new MemoryStream();
im.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
示例8: CompressImage
public static void CompressImage(Image sourceImage, int imageQuality, string savePath)
{
try
{
//Create an ImageCodecInfo-object for the codec information
ImageCodecInfo jpegCodec = null;
//Set quality factor for compression
EncoderParameter imageQualitysParameter = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, imageQuality);
//List all avaible codecs (system wide)
ImageCodecInfo[] alleCodecs = ImageCodecInfo.GetImageEncoders();
EncoderParameters codecParameter = new EncoderParameters(1);
codecParameter.Param[0] = imageQualitysParameter;
//Find and choose JPEG codec
for (int i = 0; i < alleCodecs.Length; i++)
{
if (alleCodecs[i].MimeType == "image/jpeg")
{
jpegCodec = alleCodecs[i];
break;
}
}
//Save compressed image
sourceImage.Save(HttpContext.Current.Server.MapPath(savePath), jpegCodec, codecParameter);
}
catch (Exception ex)
{
}
}
示例9: SaveImage
public void SaveImage(Image image, string fileName)
{
if (image == null) return;
image.Save(Path.Combine(ResourceManager.Instance.FavoriteImagesFolder.LocalPath, String.Format("{0}.png", fileName)));
LoadImages();
OnCollectionChanged();
}
示例10: PostImage
private static dynamic PostImage(string url, Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.ToArray();
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "image/jpeg";
request.ContentLength = bytes.Length;
Stream s = request.GetRequestStream();
s.Write(bytes, 0, bytes.Length);
s.Close();
WebResponse response = request.GetResponse();
byte[] rdata;
using (var stream = new MemoryStream())
{
response.GetResponseStream().CopyTo(stream);
rdata = stream.ToArray();
}
string resp = System.Text.Encoding.UTF8.GetString(rdata);
return JsonConvert.DeserializeObject(resp);
}
示例11: ResizeImage
public static byte[] ResizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent;
float nPercentW;
float nPercentH;
nPercentW = (size.Width / (float)sourceWidth);
nPercentH = (size.Height / (float)sourceHeight);
nPercent = nPercentH < nPercentW ? nPercentH : nPercentW;
var destWidth = (int)(sourceWidth * nPercent);
var destHeight = (int)(sourceHeight * nPercent);
var bitmap = new Bitmap(destWidth, destHeight);
var graphics = Graphics.FromImage(bitmap);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
graphics.Dispose();
using (var ms = new MemoryStream())
{
imgToResize.Save(ms, ImageFormat.Jpeg);
return ms.GetBuffer();
}
}
示例12: AddImage
public void AddImage(string name, Image image)
{
using (image)
{
string path = Path.Combine(Constants.CacheLocation, name + SaveableFileTypes.Png);
Console.WriteLine(Constants.CacheLocation, name + SaveableFileTypes.Png);
try
{
if(File.Exists(path))
{
//something may still have a handle on the previous "temp" image
//this forces it to be GC'd
GC.Collect();
GC.WaitForPendingFinalizers();
File.Delete(path);
}
image.Save(path, ImageFormat.Png);
}
catch (System.Runtime.InteropServices.ExternalException e)
{
MessageBox.Show($"Unable to add image temporary image cache. Reason: {e}");
}
catch(IOException ioe)
{
MessageBox.Show(ioe.ToString());
}
}
}
示例13: ImageToByteArray
// Convert image to post it to FB.
public byte[] ImageToByteArray(Image i_ImageIn)
{
MemoryStream imageStream = new MemoryStream();
i_ImageIn.Save(imageStream, System.Drawing.Imaging.ImageFormat.Gif);
return imageStream.ToArray();
}
示例14: SaveJpegWithCompressionSetting
/// <summary>
/// Save an Image as a JPeg with a given compression
/// Note: Filename suffix will not affect mime type which will be Jpeg.
/// </summary>
/// <param name="image">Image: Image to save</param>
/// <param name="fileName">String: File name to save the image as. Note: suffix will not affect mime type which will be Jpeg.</param>
/// <param name="compression">Long: Value between 0 and 100.</param>
private static void SaveJpegWithCompressionSetting(Image image, string fileName, long compression)
{
var eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compression);
var ici = GetEncoderInfo("image/jpeg");
image.Save(fileName, ici, eps);
}
示例15: UploadImage
public string UploadImage(Image image)
{
// TODO: maybe a global lock while we upload this image?
var fileName = RandomKey() + ".jpg";
while (_thumbnailDirectory.FileExists(fileName))
fileName = RandomKey() + ".jpg";
try
{
_thumbnailDirectory.GetFile(fileName).Open(FileMode.Create, stream => image.Save(stream, ImageFormat.Jpeg));
}
catch (Exception)
{
try
{
// if there was any issue, try to delete the file, if we created
if (_thumbnailDirectory.FileExists(fileName))
_thumbnailDirectory.DeleteFile(fileName);
}
catch (Exception)
{
// the error was more fundamental.
// don't do anything with this exception.
// let the previous exception be the real one thrown.
}
throw;
}
return fileName;
}