private Image ColorToBinary(Image img) { Bitmap bmp = new Bitmap(img); Bitmap32 bmp32 = new Bitmap32(bmp); bmp32.LockBitmap(); //bmp32.ToBlackWhite(); int of = bmp32.Offset(); for (int y = 0; y < img.Height; y++) { for (int x = 0; x < img.Width; x++) { bmp32.DrawOutLine(x, y, of); } } bmp32.UnlockBitmap(); //float[][] TempMatrix = { // new float[] {.3f, .3f, .3f, 0, 0}, // new float[] {.59f, .59f, .59f, 0, 0}, // new float[] {.11f, .11f, .11f, 0, 0}, // new float[] {0, 0, 0, 1, 0}, // new float[] {0, 0, 0, 0, 1} //}; //ColorMatrix cmatrix = new ColorMatrix(TempMatrix); //Graphics gp = Graphics.FromImage(img); //ImageAttributes imgAttri = new ImageAttributes(); //imgAttri.SetColorMatrix(cmatrix); //gp.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttri); return(bmp); }
public override void LoadTexture(Bitmap32 image) { if (renderer.MaxTextureSize > 0) { if (image.Width > renderer.MaxTextureSize) { throw new Exception(string.Format("Maximum pixel size exceeded! Width > {0}", renderer.MaxTextureSize)); } if (image.Height > renderer.MaxTextureSize) { throw new Exception(string.Format("Maximum pixel size exceeded! Height > {0}", renderer.MaxTextureSize)); } } if ((TextureSize.X > 0) && (TextureSize.Y > 0) && (TextureSize.X == image.Width) && (TextureSize.Y == image.Height)) { UpdateTexture(image); } else { LoadNewTexture(image); } TextureSize = Vector2.Create(image.Width, image.Height); }
protected override void OnDrawRequest(Bitmap32 buffer, TimeSpan elapsed) { SetTitle(string.Format("FPS: {0:0}, Particles: {1}", FramesPerSecond, m_particles.Count)); var rd = new BasicRenderer32(buffer); rd.Clear(new Color32(0x50, 0x50, 0x50, 0xFF)); foreach (var particle in m_particles) { particle.x += particle.vx * elapsed.TotalMilliseconds; particle.y += particle.vy * elapsed.TotalMilliseconds; if (particle.x < 0 || particle.x >= buffer.Width) { particle.vx = -particle.vx * 0.8; } if (particle.y < 0 || particle.y >= buffer.Height) { particle.vy = -particle.vy * 0.8; } rd.FillColor = particle.color; rd.SolidElipse((int)particle.x, (int)particle.y, (int)particle.radius, (int)particle.radius); //rd.SolidRectangle((int)(particle.x - particle.radius), (int)(particle.y - particle.radius), (int)(particle.x + particle.radius), (int)(particle.y + particle.radius)); } }
public void cutChar(Bitmap img) { Graphics gp = Graphics.FromImage(img); Bitmap32 bmp32 = new Bitmap32(img); System.Drawing.Imaging.PixelFormat format = new System.Drawing.Imaging.PixelFormat(); Point top, left, bot, right, prect; bmp32.LockBitmap(); top = bmp32.TopPoint(); left = bmp32.LeftPoint(); bot = bmp32.BotPoint(); right = bmp32.RightPoint(); //bmp32.Vector(); int of = bmp32.Offset(); for (int y = 0; y < img.Height; y++) { for (int x = 0; x < img.Width; x++) { bmp32.DrawOutLine(x, y, of); } } bmp32.UnlockBitmap(); prect = new Point(left.X, top.Y); Rectangle rect = new Rectangle(prect, new Size(right.X - left.X, bot.Y - top.Y)); Bitmap clone = img.Clone(rect, format); gp.DrawRectangle(new Pen(Color.Green), rect); picFitter.Image = clone; }
private void btnSelect_Click(object sender, EventArgs e) { Bitmap bmp = new Bitmap(28, 28); Bitmap32 bmp32 = new Bitmap32(bmp); if (int.Parse(txtNumChar.Text) < CharList.Count) { string[] CharLoad = CharList[int.Parse(txtNumChar.Text)].Split(','); bmp32.LockBitmap(); int c = 0; List <int> il = new List <int>(); for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { c = int.Parse(CharLoad[bmp.Width * y + x]); bmp32.SetPixel(x, y, Color.FromArgb(255, c, c, c)); } } bmp32.UnlockBitmap(); cutChar(bmp); bmp.SetResolution(168, 168); Bitmap bmpnew = new Bitmap(bmp, new Size(168, 168)); picHandWritten.Image = bmpnew; } }
private Image FrameDraw(Image img) { int max = img.Height; int min = img.Height / 8; Graphics gp = Graphics.FromImage(img); Bitmap bmp = new Bitmap(img); System.Drawing.Imaging.PixelFormat Format = img.PixelFormat; for (int i = max; i >= min; i = i / 2) { for (int x = 0; x < img.Width; x += i) { for (int y = 0; y < max; y += i) { Rectangle rect = new Rectangle(0, 0, i, i); Bitmap clone = bmp.Clone(rect, Format); Bitmap32 bmp32 = new Bitmap32(clone); bmp32.LockBitmap(); bmp32.Robert(r, g, b); bmp32.UnlockBitmap(); } } } return(img); }
WebImage(string fileName, string cachePath) { string ext = Path.GetExtension(fileName); FileName = fileName; MimeType = MimeTypes.FromExtension(ext); this.LogDebug("Loading Bitmap32 from file {0}", fileName); Data = File.ReadAllBytes(fileName); //read thumb if present var ThumbFileName = FileSystem.Combine(cachePath, Path.GetFileNameWithoutExtension(fileName) + "-thumb" + ext); if (File.Exists(ThumbFileName)) { //but check if the image was changed first. if (FileSystem.GetLastWriteTimeUtc(ThumbFileName) > FileSystem.GetLastWriteTimeUtc(fileName)) { ThumbData = File.ReadAllBytes(ThumbFileName); return; } } //need to recreate thumb using (var bmp = Bitmap32.Create(Data)) { switch (MimeType) { case "image/jpeg": ThumbData = RenderThumb(bmp, ThumbFileName, ImageType.Jpeg); break; case "image/png": ThumbData = RenderThumb(bmp, ThumbFileName, ImageType.Png); break; default: throw new NotImplementedException(); } } }
static void Main(string[] args) { //using (var w = new GameWindow()) //{ // w.Open(); //} var bm = Bitmap32.Load(File.ReadAllBytes("..\\..\\Sample3.png")); var random = new Random(); //var bm = new Bitmap32(640, 480); var rd = new BasicRenderer32(bm); //rd.Clear(new Color32(0x20, 0x20, 0x20, 0xFF)); rd.FillColor = new Color32(0xFF, 0x00, 0x00, 0x30); for (int i = 0; i < 100; i++) { var r = random.Next(50) + 10; var x = random.Next(bm.Width - r * 2) + r; var y = random.Next(bm.Height - r * 2) + r; rd.SolidElipse( x, y, r, r ); } //File.WriteAllBytes("out.png", bm.Save()); DisplayWindow.Show(bm); //bm.Save("out.png"); //new Bitmap32(bm, 0, 0, 128, 128).Save("out2.png"); }
private Image ColorToGrayscale(Image img) { Bitmap bmp = new Bitmap(img); Bitmap32 bmp32 = new Bitmap32(bmp); bmp32.LockBitmap(); bmp32.ToGrayScale(o); bmp32.UnlockBitmap(); return(bmp); }
private Bitmap ToFloySteinberg(Image img) { Bitmap bmp = new Bitmap(img); Bitmap32 bmp32 = new Bitmap32(bmp); bmp32.LockBitmap(); bmp32.FloydSteinberg(o); bmp32.UnlockBitmap(); return(bmp); }
private Image BlueFilter(Image img) { Bitmap bmp = new Bitmap(img); Bitmap32 bmp32 = new Bitmap32(bmp); bmp32.LockBitmap(); bmp32.GetBlue(o); bmp32.UnlockBitmap(); return(bmp); }
/// <summary>Renders a thumbnail image.</summary> /// <param name="bitmap32">The bitmap32.</param> /// <param name="thumbFileName">Name of the thumbnail file to save to.</param> /// <param name="type">The type.</param> /// <returns></returns> public static byte[] RenderThumb(Bitmap32 bitmap32, string thumbFileName, ImageType type = ImageType.Jpeg) { bitmap32.LogDebug("Creating new thumbnail {0}", thumbFileName); using (var thumbImageStream = new MemoryStream()) using (var thumbBitmap = bitmap32.Resize(300, 300, ResizeMode.TouchFromInside)) { thumbBitmap.Save(thumbImageStream, type, 50); byte[] data = thumbImageStream.ToArray(); Directory.CreateDirectory(Path.GetDirectoryName(thumbFileName)); File.WriteAllBytes(thumbFileName, data); return(data); } }
private void UpdateTexture(Bitmap32 image) { Trace.TraceInformation("Updating Texture..."); gl2.BindTexture(GL._TEXTURE_2D, texture); renderer.CheckErrors("BindTexture"); Trace.TraceInformation("retrieve raw pixels..."); int[] pixels = image.Data.Data; Trace.TraceInformation("set texture data {0}x{1} [{2}]...", image.Width, image.Height, pixels.Length); gl2.TexSubImage2D(GL._TEXTURE_2D, 0, 0, 0, image.Width, image.Height, GL._BGRA, GL._UNSIGNED_BYTE, pixels); renderer.CheckErrors("TexSubImage2D"); }
void DrawFace(int index, ARGB color, IList <Item> bmp, Bitmap32 target) { int i = index / bmp.Count; index %= bmp.Count; ARGBImageData bitmap; lock (this) { Item item = bmp[index]; bitmap = item.Bitmap.Data; } for (int n = 0; n < bitmap.Data.Length; n++) { ARGB pixel = bitmap[n]; if (pixel.Alpha == 0) { continue; } pixel.Red = (byte)(pixel.Red * color.Red / 255); pixel.Green = (byte)(pixel.Green * color.Green / 255); pixel.Blue = (byte)(pixel.Blue * color.Blue / 255); bitmap[n] = pixel; } { int x = (AvatarSize - bitmap.Width) / 2; int y = (AvatarSize - bitmap.Height) / 2; int w = bitmap.Width; int h = bitmap.Height; Translation?translation = null; switch (i) { default: case 0: // draw normal; break; case 1: // draw flipped translation = new Translation() { FlipHorizontally = true }; break; } target.Draw(bitmap, x, y, w, h, translation); Trace.TraceInformation("DrawFace {0} {1} {2} {3} {4} {5}", index, x, y, w, h, translation); } }
bool TryGetThumb(WebData webData, MDBImage img, string fileName) { try { //try to load thumb var hash = Base32.Safe.Encode(Hash.FromString(Hash.Type.SHA256, fileName)); string thumbFileName = FileSystem.Combine(mdb.CacheFolder, "Thumbs", hash + ".jpg"); var mime = MimeTypes.FromExtension(".jpg"); if (File.Exists(thumbFileName)) { try { webData.Answer = WebAnswer.Raw(webData.Request, WebMessage.Create(webData.Method, thumbFileName), File.ReadAllBytes(thumbFileName), mime); webData.Answer.SetCacheTime(TimeSpan.FromDays(1)); return(true); } catch { /*file access error, writing in progress ?, wait for lock and retry*/ } } //wait until last thumb generation is finished byte[] data; lock (ThumbCreateSyncRoot) { //do a second check after lock is released... if (File.Exists(thumbFileName)) { try { webData.Answer = WebAnswer.Raw(webData.Request, WebMessage.Create(webData.Method, thumbFileName), File.ReadAllBytes(thumbFileName), mime); webData.Answer.SetCacheTime(TimeSpan.FromDays(1)); return(true); } catch { /*file access error, recreate thumb*/ } } //generate thumb using (var bmp = Bitmap32.FromFile(fileName)) { data = WebImage.RenderThumb(bmp, thumbFileName); } } webData.Answer = WebAnswer.Raw(webData.Request, WebMessage.Create(webData.Method, thumbFileName), data, mime); webData.Answer.AllowCompression = false; webData.Answer.SetCacheTime(TimeSpan.FromDays(1)); return(true); } catch (Exception ex) { this.LogError(ex, "Could not load / create thumb for {0}", fileName); } return(false); }
IList <Item> LoadImages(string path) { var results = new List <Item>(); foreach (string file in Directory.GetFiles(path, "*.png")) { Trace.TraceInformation("Load {0}", file); var bitmap = Bitmap32.FromFile(file); results.Add(new Item() { FileName = file, Bitmap = bitmap }); } return(results); }
/// <summary>Tries to load the image data.</summary> /// <param name="data">The data.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">data</exception> public byte[] TryLoadImageData(byte[] data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } try { using (var img = Bitmap32.Create(data)) { return(data); } } catch { return(null); } }
private void ToHexArray(Image img) { Bitmap bmp = new Bitmap(img, new Size(128, 64)); bmp.SetResolution(128, 64); Bitmap32 bmp32 = new Bitmap32(bmp); bmp32.LockBitmap(); output = bmp32.ToHex(); bmp32.UnlockBitmap(); try { bmp.Save("/test.bmp"); } catch { } }
public void LoadImage(string fileName) { if (fileName != imageFileName) { try { using (Bitmap32 image = Bitmap32.FromFile(fileName)) { Sprite.LoadTexture(image); } imageFileName = fileName; } catch (Exception ex) { Trace.TraceError("Error on loading image: " + ex.Message); } } }
void DrawNose(int index, IList <Item> bmp, Bitmap32 target) { int i = index / bmp.Count; index %= bmp.Count; ARGBImageData bitmap; lock (this) { var item = bmp[index]; bitmap = item.Bitmap.Data; } int x = (AvatarSize - bitmap.Width) / 2; int y = (AvatarSize - bitmap.Height) / 2; int w, h; Translation?translation = null; switch (i) { default: case 0: // draw normal; w = bitmap.Width; h = bitmap.Height; break; case 1: // draw streched in center w = bitmap.Width * 3 / 4; h = bitmap.Height * 3 / 4; x = (AvatarSize - w) / 2; y = (AvatarSize - h) / 2; translation = new Translation() { FlipHorizontally = true }; break; } target.Draw(bitmap, x, y, w, h, translation); Trace.TraceInformation("DrawNose {0} {1} {2} {3} {4} {5}", index, x, y, w, h, translation); }
protected override void OnDrawRequest(Bitmap32 buffer, TimeSpan elapsed) { SetTitle(string.Format("FPS: {0:0}, Particles: {1}", FramesPerSecond, m_particles.Count)); var rd = new BasicRenderer32(buffer); rd.Clear(new Color32(0x50, 0x50, 0x50, 0xFF)); foreach (var particle in m_particles) { particle.x += particle.vx * elapsed.TotalMilliseconds; particle.y += particle.vy * elapsed.TotalMilliseconds; if (particle.x < 0 || particle.x >= buffer.Width) particle.vx = -particle.vx * 0.8; if (particle.y < 0 || particle.y >= buffer.Height) particle.vy = -particle.vy * 0.8; rd.FillColor = particle.color; rd.SolidElipse((int)particle.x, (int)particle.y, (int)particle.radius, (int)particle.radius); //rd.SolidRectangle((int)(particle.x - particle.radius), (int)(particle.y - particle.radius), (int)(particle.x + particle.radius), (int)(particle.y + particle.radius)); } }
/// <summary> /// Updates the texture and scale of the underlying Object3D. /// </summary> /// <param name="maxWidth">max width in pixels.</param> /// <param name="maxHeight">max height in pixels.</param> public void Update(int maxWidth = 0, int maxHeight = 0) { if (Sprite == null) { throw new ObjectDisposedException("RenderText"); } Trace.TraceInformation("Update text font:{0} size:{1} fg:{2} bg:{3} text:{4}", FontName, FontSize, ForeColor, BackColor, Text); var bitmap = Bitmap32.Create(FontName, FontSize, ForeColor, BackColor, Text); if ((maxWidth > 0) && (maxHeight > 0)) { if ((bitmap.Width > maxWidth) || (bitmap.Height > maxHeight)) { var b = new Bitmap32(Math.Min(bitmap.Width, maxWidth), Math.Min(bitmap.Height, maxHeight)); b.Draw(bitmap, 0, 0); bitmap.Dispose(); bitmap = b; } } Sprite.LoadTexture(bitmap); Sprite.Scale = Sprite.ScaleFromSize(bitmap.Width, bitmap.Height); }
void Draw(WebData data, int color, int nose, int eyes, int mouth, int face, int rotate) { using (var stream = new MemoryStream()) using (var bmp = new Bitmap32(AvatarSize, AvatarSize)) { var faceColor = ARGB.FromHSI(color / 256.0f, 1, 1); DrawFace(face, faceColor, faces, bmp); DrawEyes(eyes, this.eyes, bmp); DrawMouth(mouth, mouths, bmp); DrawNose(nose, noses, bmp); using (var result = new Bitmap32(AvatarSize, AvatarSize)) { result.Draw(bmp, 0, 0, new Translation() { Rotation = ((rotate % 16) - 7) * 0.02f }); result.Save(stream); } var msg = WebMessage.Create(data.Method, "Avatar created"); data.Answer = WebAnswer.Raw(data.Request, msg, stream.GetBuffer(), "image/png"); data.Answer.AllowCompression = false; data.Answer.SetCacheTime(TimeSpan.FromDays(1)); } }
bool SaveImage(byte[] data, MDBFolder mdbFolder, string name, ref MDBImage image, object obj) { string fullPath; var file = new MDBFile() { FolderID = mdbFolder.ID, Name = name, }; ImageType imgType; switch (image.Type) { case MDBImageType.ArtistMusicBanner: case MDBImageType.ArtistMusicLogo: case MDBImageType.ArtistMusicLogoHD: case MDBImageType.AlbumCDArt: fullPath = mdbFolder.GetFullPath(this, name + ".png"); file.Extension = ".png"; file.FileType = MDBFileType.png; imgType = ImageType.Png; image.MimeType = "image/png"; break; default: fullPath = mdbFolder.GetFullPath(this, name + ".jpg"); file.Extension = ".jpg"; imgType = ImageType.Jpeg; file.FileType = MDBFileType.jpeg; image.MimeType = "image/jpg"; break; } int width, height; using (var img = Bitmap32.Create(data)) { width = img.Width; height = img.Height; //save if not present at disk if (!File.Exists(fullPath)) { using (var ms = new MemoryStream()) { img.Save(ms, imgType, 99); data = ms.ToArray(); } } } bool writeFile = true; //find old dataset (check for replace) { if (TryGetFile(fullPath, false, out MDBFile mdbFile)) { file.ID = mdbFile.ID; if (mdbFile.GetFullPath(this) == fullPath) { writeFile = false; } else { string oldPath = mdbFile.GetFullPath(this); File.Delete(oldPath); } } } //save image data if (writeFile) { foreach (string oldFile in Directory.GetFiles(Path.GetDirectoryName(fullPath), Path.GetFileNameWithoutExtension(fullPath) + ".*")) { File.Delete(oldFile); if (TryGetFile(oldFile, false, out MDBFile mdbFile)) { Files.Delete(mdbFile.ID); } this.LogInfo("Deleted old file {0}", oldFile); } File.WriteAllBytes(fullPath, data); this.LogInfo("Saved new image {0}", fullPath); } //get fileinfo var fileInfo = new FileInfo(fullPath); //create file dataset file.DateTime = fileInfo.LastWriteTimeUtc; file.Size = fileInfo.Length; if (file.ID > 0) { Files.Replace(file); } else { file.ID = Files.Insert(file); } //update image dataset image.Width = width; image.Height = height; image.FileID = file.ID; if (Images.Exist(file.ID)) { Images.Replace(image); this.LogNotice("<cyan>Update {0} image<default> dataset for <yellow>{1} <default>{2}", image, obj, mdbFolder); return(false); } else { Images.Insert(image); this.LogNotice("<green>New {0} image<default> dataset for <yellow>{1} <default>{2}", image, obj, mdbFolder); return(true); } }
/// <summary> /// Loads a new texture bitmap. /// </summary> /// <param name="bitmap">The bitmap to load.</param> public abstract void LoadTexture(Bitmap32 bitmap);