/// <summary> /// Images to byte array. /// </summary> /// <param name="image">The image in.</param> /// <param name="extension">The extension.</param> /// <param name="encoderParameters">The encoder parameters.</param> /// <returns></returns> public static byte[] ImageToByteArray(System.Drawing.Image image, string extension, EncoderParameters encoderParameters) { MemoryStream ms = new MemoryStream(); if (!string.IsNullOrEmpty(extension) && encoderParameters != null) image.Save(ms, GetImageCodec(extension), encoderParameters); else image.Save(ms, image.RawFormat); return ms.ToArray(); }
private static Gdk.Pixbuf ImageToPixbuf(System.Drawing.Image image) { using (MemoryStream stream = new MemoryStream()) { image.Save("test.png", ImageFormat.Png); image.Save(stream, ImageFormat.Png); stream.Position = 0; Gdk.Pixbuf pixbuf = new Gdk.Pixbuf(stream); return pixbuf; } }
static public byte[] imageToByteArray(System.Drawing.Image imageIn) { using (var ms = new MemoryStream()) { try { imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); } catch { imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.MemoryBmp); } return ms.ToArray(); } }
public static BitmapImage bitmap2bitmapImage(System.Drawing.Bitmap image) { if (image == null) { return null; } BitmapImage bitmapImage = new BitmapImage(); try { using (MemoryStream memory = new MemoryStream()) { image.Save(memory, System.Drawing.Imaging.ImageFormat.Png); memory.Position = 0; bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); } } catch (Exception ex) { Console.WriteLine(ex.Message); return null; } return bitmapImage; }
public static void Set(System.Drawing.Image img, Style style) { // System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString()); // System.Drawing.Image img = System.Drawing.Image.FromStream(s); string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp"); img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp); RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true); if (style == Style.Stretched) { key.SetValue(@"WallpaperStyle", 2.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } if (style == Style.Centered) { key.SetValue(@"WallpaperStyle", 1.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } if (style == Style.Tiled) { key.SetValue(@"WallpaperStyle", 1.ToString()); key.SetValue(@"TileWallpaper", 1.ToString()); } NativeMethods.SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, tempPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE); }
// http://stackoverflow.com/questions/220465/using-256-x-256-vista-icon-in-application/1945764#1945764 // Based on: http://www.codeproject.com/KB/cs/IconExtractor.aspx // And a hint from: http://www.codeproject.com/KB/cs/IconLib.aspx public static System.Drawing.Bitmap ExtractVistaIcon(System.Drawing.Icon icoIcon) { System.Drawing.Bitmap bmpPngExtracted = null; try { byte[] srcBuf = null; using (MemoryStream stream = new MemoryStream()) { icoIcon.Save(stream); srcBuf = stream.ToArray(); } const int SizeICONDIR = 6; const int SizeICONDIRENTRY = 16; int iCount = BitConverter.ToInt16(srcBuf, 4); for (int iIndex = 0; iIndex < iCount; iIndex++) { int iWidth = srcBuf[SizeICONDIR + SizeICONDIRENTRY * iIndex]; int iHeight = srcBuf[SizeICONDIR + SizeICONDIRENTRY * iIndex + 1]; int iBitCount = BitConverter.ToInt16(srcBuf, SizeICONDIR + SizeICONDIRENTRY * iIndex + 6); if (iWidth == 0 && iHeight == 0 && iBitCount == 32) { int iImageSize = BitConverter.ToInt32(srcBuf, SizeICONDIR + SizeICONDIRENTRY * iIndex + 8); int iImageOffset = BitConverter.ToInt32(srcBuf, SizeICONDIR + SizeICONDIRENTRY * iIndex + 12); MemoryStream destStream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(destStream); writer.Write(srcBuf, iImageOffset, iImageSize); destStream.Seek(0, SeekOrigin.Begin); bmpPngExtracted = new System.Drawing.Bitmap(destStream); // This is PNG! :) break; } } } catch { return null; } return bmpPngExtracted; }
/* public static Texture2D BitmapToTexture2D(GraphicsDevice device, System.Drawing.Bitmap image) { //Buffer size is size of color array multiplied by 4 because each pixel has four color bytes int bufferSize = image.Height * image.Width * 4; // Create new memory stream and save image to stream so wo don't have to save and read file System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bufferSize); image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); // Creates a texture from IO.stream - our memory stream image.setdata Texture2D texture=Texture2D.FromStream(device,memoryStream); texture.SetData return texture; } */ public Texture2D BitmapToTexture2D(GraphicsDevice device, System.Drawing.Bitmap bmp) { /* Color[] pixels = new Color[bmp.Width * bmp.Height]; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { System.Drawing.Color c = bmp.GetPixel(x, y); pixels[(y * bmp.Width) + x] = new Color(c.R, c.G, c.B, c.A); } } Texture2D mytex = new Texture2D(device, bmp.Width, bmp.Height, 1,TextureUsage.None, SurfaceFormat.Color); mytex.SetData<Color>(pixels); return mytex; */ using (MemoryStream s = new MemoryStream()) { bmp.Save(s, System.Drawing.Imaging.ImageFormat.Png); s.Seek(0, SeekOrigin.Begin); Texture2D tx = Texture2D.FromFile(device, s); return tx; } }
public static string ImageToBase64String(System.Drawing.Image imageIn) { MemoryStream ms = new MemoryStream(); imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); return Convert.ToBase64String(ms.ToArray()); ; }
/// <summary> /// 保存本地缓存 /// </summary> /// <param name="imageuri"></param> /// <param name="imgImage"></param> public static void CachoImage(string imageuri, System.Drawing.Image imgImage) { #region 缓存本地文件 if (!string.IsNullOrEmpty(imageuri)) { if (imgImage != null) { if (!string.IsNullOrEmpty(PublicStatic.LiuXingVideoImageCacheDir)) { var imageurimd5 = XunLeiLoginHelper.GetMd5Encoding(imageuri); if (!string.IsNullOrEmpty(imageurimd5)) { if (!System.IO.Directory.Exists(PublicStatic.LiuXingVideoImageCacheDir)) { System.IO.Directory.CreateDirectory(PublicStatic.LiuXingVideoImageCacheDir); } try { imgImage.Save(PublicStatic.LiuXingVideoImageCacheDir + imageurimd5 + ".kimage"); } catch { } } } } } #endregion }
private byte[] ImageToByteArray(System.Drawing.Image m_imageIn) { MemoryStream oMemoryStream = new MemoryStream(); // ImageFormat could be other formats like bmp,gif,icon,png etc. m_imageIn.Save(oMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg); return oMemoryStream.ToArray(); }
public bool SaveImageForSpecifiedQuality(System.Drawing.Image sourceImage, string savePath, int imageQualityValue) { //以下代码为保存图片时,设置压缩质量 EncoderParameters encoderParameters = new EncoderParameters(); EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, imageQualityValue); encoderParameters.Param[0] = encoderParameter; try { ImageCodecInfo[] ImageCodecInfoArray = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegImageCodecInfo = null; for (int i = 0; i < ImageCodecInfoArray.Length; i++) { if (ImageCodecInfoArray[i].FormatDescription.Equals("JPEG")) { jpegImageCodecInfo = ImageCodecInfoArray[i]; break; } } sourceImage.Save(savePath, jpegImageCodecInfo, encoderParameters); return true; } catch { return false; } }
private static Texture2D Bitmap2Texture2D(System.Drawing.Bitmap bitmap) { var s = new MemoryStream(); bitmap.Save(s, System.Drawing.Imaging.ImageFormat.Png); var t = Sprite.CreateTextureFromStream(s); return t; }
public byte[] BmpToBytes(System.Drawing.Image bmp) { MemoryStream ms = null; byte[] bmpBytes = null; try { ms = new MemoryStream(); // Save to memory using the Jpeg format bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); // read to end bmpBytes = ms.GetBuffer(); } catch { return null; } finally { bmp.Dispose(); if (ms != null) { ms.Close(); } } return bmpBytes; }
public static void GetBarcode(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image, string fileSaveUrl) { try { image = null; BarcodeLib.Barcode b = new BarcodeLib.Barcode(); b.BackColor = System.Drawing.Color.White;//图片背景颜色 b.ForeColor = System.Drawing.Color.Black;//条码颜色 b.IncludeLabel = true; b.Alignment = BarcodeLib.AlignmentPositions.LEFT; b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;//code的显示位置 b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;//图片格式 System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);//字体设置 b.LabelFont = font; b.Height = height;//图片高度设置(px单位) b.Width = width;//图片宽度设置(px单位) image = b.Encode(type, code);//生成图片 image.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception err) { err.ToString(); image = null; } }
// TODO is there a less brute-force way? public static Gdk.Pixbuf Bitmap2Pixbuf(System.Drawing.Bitmap bitmap) { MemoryStream ms = new MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ms.Seek(0, SeekOrigin.Begin); return new Gdk.Pixbuf(ms); }
/// <summary> /// Converts Bitmap to BitmapSource /// </summary> /// <param name="bitmap"></param> /// <returns></returns> public static BitmapSource CreateBitmapSourceFromBitmap(System.Drawing.Image bitmap) { if (bitmap == null) throw new ArgumentNullException("bitmap"); if (System.Windows.Application.Current.Dispatcher == null) return null; // Is it possible? try { using (MemoryStream memoryStream = new MemoryStream()) { // You need to specify the image format to fill the stream. // I'm assuming it is PNG bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); memoryStream.Seek(0, SeekOrigin.Begin); // Make sure to create the bitmap in the UI thread return (BitmapSource)System.Windows.Application.Current.Dispatcher.Invoke( new Func<Stream, BitmapSource>(CreateBitmapSourceFromBitmap), System.Windows.Threading.DispatcherPriority.Normal, memoryStream); } } catch (Exception) { return null; } }
public static string IconToString(System.Drawing.Image icon) { var ms = new System.IO.MemoryStream(); icon.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ms.Position = 0; return (new System.IO.StreamReader(ms)).ReadToEnd(); }
public string ReadText(System.Drawing.Bitmap image) { string result = null; // This line fails in Azure deployment //IntPtr hBitmap = image.GetHbitmap(); //engine.ReadFromBitmap((int)hBitmap, 0); // Reading from memory stream as opposed to gdi bitmap as deployment of gdi to Azure is problematic var memStream = new MemoryStream(); image.Save(memStream, GetImageFormat(image.RawFormat.Guid)); memStream.Position = 0; var buffer = memStream.ToArray(); engine.ReadFromMemFile(buffer, buffer.Length, 0); //DeleteObject(hBitmap); //HttpContext.Current.Response.Write("Jelly Test 2 ### " + result); for (int i = 0; i < engine.Plates.Count; i++) { Plate plate = engine.Plates.get_Item(i); result = plate.Text; Marshal.ReleaseComObject(plate); plate = null; } if (engine.Plates.Count == 0) result = "*no match*"; memStream.Dispose(); GC.Collect(); return result; }
public static modProgram.sResult SaveBitmap(string Path, bool Overwrite, System.Drawing.Bitmap BitmapToSave) { modProgram.sResult result; result.Problem = ""; result.Success = false; try { if (File.Exists(Path)) { if (!Overwrite) { result.Problem = "File already exists."; return result; } File.Delete(Path); } BitmapToSave.Save(Path); } catch (Exception exception1) { ProjectData.SetProjectError(exception1); Exception exception = exception1; result.Problem = exception.Message; modProgram.sResult result2 = result; ProjectData.ClearProjectError(); return result2; } result.Success = true; return result; }
public Edit(System.Drawing.Bitmap bmp) { // Convert to bitmapimage using (MemoryStream memory = new MemoryStream()) { bmp.Save(memory, ImageFormat.Png); memory.Position = 0; bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); } elements = new Stack<UIElement>(); redos = new Stack<UIElement>(); this.originalScreenshot = new CroppedBitmap(bitmapImage, new Int32Rect(0, 0, bitmapImage.PixelWidth, bitmapImage.PixelHeight)); this.bmpScreenshot = originalScreenshot; InitializeComponent(); imgViewer.Background = new ImageBrush(this.bmpScreenshot); textBox_Width.Text = bmpScreenshot.Width.ToString(); textBox_Height.Text = bmpScreenshot.Height.ToString(); WindowStyle = WindowStyle.SingleBorderWindow; //Topmost = true; WindowState = WindowState.Maximized; btn_Redo.IsEnabled = false; btn_Undo.IsEnabled = false; highlightColor = new Color(); this.originalHeight = imgViewer.Height; this.originalWidth = imgViewer.Width; initialize(); }
protected void WriteGCode(System.Drawing.Bitmap b) { Bitmap = b; SizeX = b.Width; SizeY = b.Height; PixelSizeX = 25.4 / b.HorizontalResolution; PixelSizeY = 25.4 / b.VerticalResolution; ShiftX = (double)LoadOptions.LaserSize / 2.0; ShiftY = (double)LoadOptions.LaserSize / 2.0; if (!string.IsNullOrEmpty(LoadOptions.ImageWriteToFileName)) b.Save(LoadOptions.ImageWriteToFileName, System.Drawing.Imaging.ImageFormat.Bmp); AddComment("Image.Width", b.Width); AddComment("Image.Height", b.Width); AddComment("Image.HorizontalResolution(DPI)", b.HorizontalResolution); AddComment("Image.VerticalResolution(DPI)", b.VerticalResolution); AddComment("ImageInvert", LoadOptions.ImageInvert.ToString()); if (LoadOptions.MoveSpeed.HasValue) { var setspeed = new G01Command(); setspeed.AddVariable('F', LoadOptions.MoveSpeed.Value); Commands.Add(setspeed); } WriteGCode(); }
/// <summary> /// this function modifies a window shape /// </summary> /// <param name="bmp"> /// the bitmap that has the shape of the window. <see cref="System.Drawing.Bitmap"/> /// </param> /// <param name="window"> /// the window to apply the shape change <see cref="Gtk.Window"/> /// </param> /// <returns> /// true if it succeded, either return false <see cref="System.Boolean"/> /// </returns> public static bool ModifyWindowShape( System.Drawing.Bitmap bmp, Gtk.Window window ) { bool ret = false; try { //save bitmap to stream System.IO.MemoryStream stream = new System.IO.MemoryStream(); bmp.Save( stream, System.Drawing.Imaging.ImageFormat.Png ); //verry important: put stream on position 0 stream.Position = 0; //get the pixmap mask Gdk.Pixbuf buf = new Gdk.Pixbuf( stream, bmp.Width, bmp.Height ); Gdk.Pixmap map1, map2; buf.RenderPixmapAndMask( out map1, out map2, 255 ); //shape combine window window.ShapeCombineMask( map2, 0, 0 ); //dispose buf.Dispose(); map1.Dispose(); map2.Dispose(); bmp.Dispose(); //if evrything is ok return true; ret = true; } catch(Exception ex) { Console.WriteLine( ex.Message + "\r\n" + ex.StackTrace ); } return ret; }
private static byte[] imageToByteArray(System.Drawing.Bitmap imageIn) { var ms = new MemoryStream(); imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png); return ms.ToArray(); }
//------------------------------------------------------------ //------------------------------------------------------------ private static byte[] imageToByteArray(System.Drawing.Image image) { MemoryStream ms = new MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); return ms.ToArray(); }
public override void Draw(System.Drawing.Graphics g) { #if DEBUG Console.WriteLine("Zone"); #endif GraphicsState gState = g.Save(); //Matrix scaleM = new Matrix(); //scaleM.Scale(1, 1); //scaleM.TransformPoints(arrPoints); base.translationMatrix.Reset(); base.translationMatrix.Translate(this.Position.X, this.Position.Y); g.MultiplyTransform(base.translationMatrix, MatrixOrder.Prepend); g.FillRectangle(Brushes.Blue, 0, 0, 500, 250); //g.FillRectangle(Brushes.Blue, 0, 0, 500, 250); foreach (IDrawable e in cards) { if (e.Visible) { e.Draw(g); } } g.Restore(gState); }
private byte[] GetBytesFromBmp(System.Drawing.Image img, int visibleWidth, int visibleHeight) { byte[] imgBytes = new byte[visibleWidth * 3 * visibleHeight]; //since bmp format requests certain image width (of multiples of 4) we have to take padding into account var padding = (img.Width % 4); padding = padding == 0 ? 0 : 4 - padding; var bitmapHeaderOffset = 54; using (var ms = new System.IO.MemoryStream()) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); ms.Seek(bitmapHeaderOffset, System.IO.SeekOrigin.Begin); if (img.Height - visibleHeight > 0) { //3 for rgb image (1 byte for each color channel) ms.Seek((3 * img.Width + padding) * (img.Height - visibleHeight), System.IO.SeekOrigin.Current); } for (int i = 0; i < visibleHeight; i++) { ms.Read(imgBytes, i * visibleWidth * 3, visibleWidth * 3); ms.Seek(padding, System.IO.SeekOrigin.Current); ms.Seek((img.Width - visibleWidth) * 3, System.IO.SeekOrigin.Current); } } return imgBytes.Where((x, i) => (i + 1) % 4 != 255).ToArray(); }
private static void SaveSection(System.Configuration.Configuration config, ConfigurationSection section) { // Save the section. section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); }
public void Save_False_To_Mute_Loads_False_To_GameState_Param() { GameState.Mute = false; System.Save(GameState); Assert.AreEqual(false, System.Load().Mute); }
/* Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed Server.MapPath("..") returns the parent directory Server.MapPath("~") returns the physical path to the root of the application Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application) */ public byte[] imageToByteArray(System.Drawing.Image imageIn) { using (var ms = new MemoryStream()) { imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); return ms.ToArray(); } }
public static byte[] ImageToByteArray(System.Drawing.Image image) { using (MemoryStream memoryStream = new MemoryStream()) { image.Save(memoryStream, image.RawFormat); return memoryStream.ToArray(); } }
public void Save_5_To_TimeRemaining_Loads_5_To_GameState_Param() { GameState.TimeRemaining = 5; System.Save(GameState); Assert.AreEqual(5, System.Load().TimeRemaining); }
public void Save_1f_To_MusicVolume_Loads_1f_To_GameState_Param() { GameState.MusicVolume = 0.3f; System.Save(GameState); Assert.AreEqual(0.3f, System.Load().MusicVolume); }
public void Save_25_To_Score_Loads_25_To_GameState_Param() { GameState.Score = 25; System.Save(GameState); Assert.AreEqual(25, System.Load().Score); }
public void Draw(System.Drawing.Graphics g) { GraphicsState gs = g.Save(); g.FillRectangle(new SolidBrush(Color.FromArgb(127, Color.LightBlue)), HighlightRectangle); g.Restore(gs); }
public void Save_John_To_PlayerName_Loads_John_To_GameState_Param() { GameState.PlayerName = "John"; System.Save(GameState); Assert.AreEqual("John", System.Load().PlayerName); }