//Draw selected icon to render private void drawIcon() { Bitmap drawBitmap = new Bitmap(177, 177); Graphics drawGraphics = Graphics.FromImage(drawBitmap); Pen blackPen = new Pen(Color.Black); //Load icon data loadIcons(); drawGraphics.PixelOffsetMode = PixelOffsetMode.Half; drawGraphics.InterpolationMode = InterpolationMode.NearestNeighbor; //Draw selected icon to drawBitmap drawGraphics.DrawImage(iconBitmap[selectedIcon], 0, 0, 177, 177); //Set offset mode to default so grid can be drawn drawGraphics.PixelOffsetMode = PixelOffsetMode.Default; //Draw grid for (int y = 0; y < 17; y++) { drawGraphics.DrawLine(blackPen, 0, (y * 11), 177, (y * 11)); } for (int x = 0; x < 17; x++) { drawGraphics.DrawLine(blackPen, (x * 11), 0, (x * 11), 177); } drawGraphics.Dispose(); drawBitmap.Save("temp.bmp", System.Drawing.Imaging.ImageFormat.Bmp); IconImage = new Avalonia.Media.Imaging.Bitmap("temp.bmp"); }
public byte[] ImageToByteArray(Avalonia.Media.Imaging.Bitmap image) { using (MemoryStream stream = new MemoryStream()) { image.Save(stream); return(stream.ToArray()); } }
private string ImageToBase64(Avalonia.Media.Imaging.Bitmap image) { using (MemoryStream stream = new MemoryStream()) { image.Save(stream); return(Convert.ToBase64String(stream.ToArray())); } }
public static System.Drawing.Image ToDrawingImage(this Avalonia.Media.Imaging.Bitmap bmp) { System.Drawing.Image ret; using (MemoryStream stream = new MemoryStream()) { bmp.Save(stream); ret = System.Drawing.Image.FromStream(stream); } return(ret); }
public void init(string F, string N, string C, string S, int mol, List <string> vs) { TextBox text = this.FindControl <TextBox>("text"); text.Text = string.Join("\n\n", new List <string> { "化学式: " + F, "名称: " + N, "CAS 号: " + (C == "" || C == null ? "未知" : C), "分子质量: " + (mol / 10.0).ToString("N1") + " g/mol", "SMILES: " + S, "贡献者: " + string.Join(", ", vs) }); string can = ""; foreach (char ch in S) { int hex = ch; can += "%" + Convert.ToString(hex, 16); } try { // 使用 KingDraw 的 API 将 SMILES 转换为图片 WebResponse response = WebRequest.Create("http://baike.kingdraw.com/api/smilesToImage?smiles=" + can).GetResponse(); Stream stream = response.GetResponseStream(); System.Drawing.Image image = System.Drawing.Image.FromStream(stream); Bitmap bitmap = new Bitmap(image); if (image.Width > 280) { bitmap = ZoomImage(bitmap, image.Height * 280 / image.Width, 280); } Color clr; for (int x = 0; x < bitmap.Width; ++x) { for (int y = 0; y < bitmap.Height; ++y) { clr = bitmap.GetPixel(x, y); bitmap.SetPixel(x, y, Color.FromArgb(Math.Max(40, 255 - clr.R), Math.Max(40, 255 - clr.G), Math.Max(40, 255 - clr.B))); } } Stream str = new MemoryStream(); bitmap.Save(str, ImageFormat.Png); str.Position = 0; Avalonia.Media.Imaging.Bitmap bitmap1 = new Avalonia.Media.Imaging.Bitmap(str); this.FindControl <Avalonia.Controls.Image>("img").Source = bitmap1; Height += bitmap1.Size.Height; this.FindControl <Avalonia.Controls.Image>("img").Height = bitmap1.Size.Height; this.FindControl <Avalonia.Controls.Image>("img").Width = bitmap1.Size.Width; } catch (Exception) { } }
//Draw palette image to render private void drawPalette() { Bitmap paletteBitmap = new Bitmap(8, 2); Bitmap drawBitmap = new Bitmap(121, 31); Graphics drawGraphics = Graphics.FromImage(drawBitmap); Pen blackPen = new Pen(Color.Black); int colorCounter = 0; //Load pallete data loadPalette(); drawGraphics.PixelOffsetMode = PixelOffsetMode.Half; drawGraphics.InterpolationMode = InterpolationMode.NearestNeighbor; //Plot pixels onto bitmap for (int y = 0; y < 2; y++) { for (int x = 0; x < 8; x++) { paletteBitmap.SetPixel(x, y, iconPalette[colorCounter]); colorCounter++; } } //Draw palette to drawBitmap drawGraphics.DrawImage(paletteBitmap, 0, 0, 120, 30); //Set offset mode to default so grid can be drawn drawGraphics.PixelOffsetMode = PixelOffsetMode.Default; //Draw grid for (int y = 0; y < 3; y++) { drawGraphics.DrawLine(blackPen, 0, (y * 15), 121, (y * 15)); } for (int x = 0; x < 9; x++) { drawGraphics.DrawLine(blackPen, (x * 15), 0, (x * 15), 31); } drawGraphics.Dispose(); drawBitmap.Save("temp.bmp", System.Drawing.Imaging.ImageFormat.Bmp); PaletteImage = new Avalonia.Media.Imaging.Bitmap("temp.bmp"); }
/// <summary> /// データのダウンロード完了後イベントハンドラ /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ClientDownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) { try { byte[] bytes = e.Result; Stream stream = new MemoryStream(bytes); var image = new Avalonia.Media.Imaging.Bitmap(stream); var bitmapProperty = (ReactivePropertySlim <Avalonia.Media.Imaging.Bitmap>?)e.UserState; bitmapProperty !.Value = image; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } }
private async void InfoWindow_Opened(object sender, EventArgs e) { await Task.Delay(100); try { if (item.FileFormat == FileFormat.SevenZip) { throw new Exception("Can't load from compressed files."); } var filePath = Path.Combine(item.FullFolderPath, item.ImageFile); var gdtexture = await Task.Run(() => ImageHelper.GetGdText(filePath)); if (gdtexture == null) { throw new Exception("File not found"); } else { var decoded = await Task.Run(() => new PuyoTools.PvrTexture().GetDecoded(gdtexture)); using (var memory = new MemoryStream()) { byte[] data = decoded.Item1; using (var writeableBitmap = new Avalonia.Media.Imaging.WriteableBitmap(new PixelSize(decoded.Item2, decoded.Item3), new Vector(96, 96), Avalonia.Platform.PixelFormat.Bgra8888, Avalonia.Platform.AlphaFormat.Unpremul)) { using (var l = writeableBitmap.Lock()) System.Runtime.InteropServices.Marshal.Copy(data, 0, l.Address, data.Length); writeableBitmap.Save(memory); memory.Position = 0; GdTexture = new Avalonia.Media.Imaging.Bitmap(memory); LabelText = null; } } } } catch (Exception ex) { LabelText = ex.Message; } }
public ExportIdentityViewModel() { this.QRImage = null; this.Title = _loc.GetLocalizationValue("ExportIdentityWindowTitle"); this.Identity = _identityManager.CurrentIdentity; QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(this.Identity.ToByteArray(), QRCodeGenerator.ECCLevel.H); QRCode qrCode = new QRCode(qrCodeData); var assets = AvaloniaLocator.Current.GetService <IAssetLoader>(); var bpm = new System.Drawing.Bitmap(assets.Open(new Uri("resm:SQRLDotNetClientUI.Assets.SQRL_icon_normal_32.png"))); var bitMap = qrCode.GetGraphic(3, System.Drawing.Color.Black, System.Drawing.Color.White, bpm, 15, 1); var temp = System.IO.Path.GetTempFileName(); bitMap.Save(temp); this.QRImage = new Avalonia.Media.Imaging.Bitmap(temp); }
/// <summary> /// Converts an ImageSharp Image to Avalonia Bitmap, for rendering. /// When possible, avoid this! For static resources in XAML, use the "resm:" URI. /// </summary> /// <param name="resource">Image resource bytes.</param> /// <returns>Avalonia Bitmap.</returns> public static Avalonia.Media.Imaging.Bitmap ToAvaloniaBitmap(this Image <Rgba32> image) { string tempFile = Path.GetTempFileName(); try { using (FileStream stream = File.OpenWrite(tempFile)) { image.SaveAsPng(stream); } var bmp = new Avalonia.Media.Imaging.Bitmap(tempFile); return(bmp); } finally { File.Delete(tempFile); } }
public SkinColorEdit() { DataContext = this; InitializeComponent(); ColorButton.Click += ColorEdit_Click; ColorCanvas.PointerPressed += ColorCanvas_PointerPressed; ColorCanvas.PointerMoved += ColorCanvas_PointerMoved; ColorThumb.DragDelta += ColorThumb_DragDelta; NumericSkinColorX.ValueChanged += (sender, e) => { SkinColorX = (byte)e.NewValue; }; NumericSkinColorY.ValueChanged += (sender, e) => { SkinColorY = (byte)e.NewValue; }; var imgSource = Locator.Current.GetService <AssetsService>() !.SkinColorPath; var image = new Avalonia.Media.Imaging.Bitmap(imgSource); Dispatcher.UIThread.Post(() => ColorCanvas.Background = new ImageBrush(image)); // Backend bitmap skinImage = SixLabors.ImageSharp.Image.Load <Rgba32>(imgSource); UpdatePreviewColor(); }
public static Avalonia.Media.Imaging.Bitmap ToMediaBitmap(this System.Drawing.Bitmap bmp) { Avalonia.Media.Imaging.Bitmap ret; /*var data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); * using (MemoryStream stream = new MemoryStream()) * { * bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png); * ret = new Avalonia.Media.Imaging.Bitmap(stream); * } * return ret;*/ using (var stream = new MemoryStream()) { bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Position = 0; //var factory = Avalonia.AvaloniaLocator.Current.GetService(typeof(Avalonia.Platform.IPlatformRenderInterface)); //ret = factory.LoadBitmap(stream); ret = new Avalonia.Media.Imaging.Bitmap(stream); } return(ret); }
private void DoSearch(string s) { WebResponse response = null; string can = ""; foreach (char ch in s) { int hex = ch; can += "%" + Convert.ToString(hex, 16); } try { // 使用 KingDraw 的 API 将 SMILES 转换为图片 response = WebRequest.Create("http://baike.kingdraw.com/api/smilesToImage?smiles=" + can).GetResponse(); } catch (Exception) { Height = 85; this.FindControl <Avalonia.Controls.Image>("img").IsVisible = false; this.FindControl <Button>("save").IsVisible = false; this.FindControl <TextBlock>("err").IsVisible = false; this.FindControl <TextBlock>("nerr").IsVisible = true; return; } try { Stream stream = response.GetResponseStream(); System.Drawing.Image image = System.Drawing.Image.FromStream(stream); bitmap = new Bitmap(image); nbitmap = (Bitmap)bitmap.Clone(); if (image.Width > Width - 20) { bitmap = InfoWindow.ZoomImage(bitmap, (int)(image.Height * (Width - 20) / image.Width), (int)(Width - 20)); } Color clr; for (int x = 0; x < bitmap.Width; ++x) { for (int y = 0; y < bitmap.Height; ++y) { clr = bitmap.GetPixel(x, y); bitmap.SetPixel(x, y, Color.FromArgb(Math.Max(40, 255 - clr.R), Math.Max(40, 255 - clr.G), Math.Max(40, 255 - clr.B))); } } Stream str = new MemoryStream(); bitmap.Save(str, ImageFormat.Png); str.Position = 0; Avalonia.Media.Imaging.Bitmap bitmap1 = new Avalonia.Media.Imaging.Bitmap(str); this.FindControl <Avalonia.Controls.Image>("img").Source = bitmap1; Height = 50; Height += bitmap1.Size.Height + 50; this.FindControl <Avalonia.Controls.Image>("img").Height = bitmap1.Size.Height; this.FindControl <Avalonia.Controls.Image>("img").Width = bitmap1.Size.Width; this.FindControl <Avalonia.Controls.Image>("img").IsVisible = true; this.FindControl <Button>("save").IsVisible = true; this.FindControl <TextBlock>("err").IsVisible = false; this.FindControl <TextBlock>("nerr").IsVisible = false; } catch (Exception) { Height = 85; this.FindControl <Avalonia.Controls.Image>("img").IsVisible = false; this.FindControl <Button>("save").IsVisible = false; this.FindControl <TextBlock>("err").IsVisible = true; this.FindControl <TextBlock>("nerr").IsVisible = false; } }
public static System.Drawing.Bitmap ToDrawingBitmap(this Avalonia.Media.Imaging.Bitmap bmp) => (System.Drawing.Bitmap)bmp.ToDrawingImage();