/// <summary> /// Checks if resource is really image and if it exists /// </summary> /// <param name="handle"></param> /// <returns></returns> /// <exception cref="PhpException">Warning when resource is not valid <see cref="PhpGdImageResource"/>.</exception> internal static PhpGdImageResource ValidImage(PhpResource handle) { PhpGdImageResource result = handle as PhpGdImageResource; if (result != null && result.IsValid && result.image != null) { return(result); } PhpException.Throw(PhpError.Warning, Utils.Resources.GetString("image_resource_not_valid")); return(null); }
/// <summary> /// Tries to load image from local file or from URL and checks its format for match if specified. /// </summary> /// <param name="filename"></param> /// <param name="format"></param> /// <returns></returns> private static PhpResource CreateGdImageFrom(string filename, ImageFormat format) { if (string.IsNullOrEmpty(filename)) { PhpException.Throw(PhpError.Warning, Utils.Resources.GetString("filename_cannot_be_empty")); return null; } Bitmap image = LoadBitmap(filename, format); if (image == null) return null; var result = new PhpGdImageResource(image); var color = image.Palette.Entries.Where(a => a.A < 255).Take(1).ToArray(); if (color.Length > 0) { result.transparentColor = color[0]; result.IsTransparentColSet = true; result.SaveAlpha = true; } return result; }
private static Color GetAlphaColor(PhpGdImageResource img, int col) { Color color; if (!img.AlphaBlending) { color = Color.FromArgb(255, PHPColorToRed(col), PHPColorToGreen(col), PHPColorToBlue(col)); } else { color = PHPColorToNETColor(col); } return color; }
public static PhpResource imagerotate(PhpResource im, double angle, int bgdcolor, int ignoretransparent) { if (ignoretransparent != 0) { PhpException.ArgumentValueNotSupported("ignoretransparent", ignoretransparent); } PhpGdImageResource img = PhpGdImageResource.ValidImage(im); if (img == null) return null; if (angle < 360) angle = angle - ((int)angle / 360) * 360; //if (angle < 0) angle = 360 + angle; PhpGdImageResource ret_im = new PhpGdImageResource(RotateImage(img.Image, -angle, PHPColorToNETColor(bgdcolor))); /*var graphics = Graphics.FromImage(ret_im.Image); SolidBrush brush = new SolidBrush(Color.FromArgb(bgdcolor)); graphics.FillRectangle(brush, 0, 0, ret_im.Image.Width, ret_im.Image.Height); graphics.TranslateTransform((float)img.Image.Width / 2, (float)img.Image.Height / 2); graphics.RotateTransform(-angle); graphics.TranslateTransform(-(float)img.Image.Width / 2, -(float)img.Image.Height / 2); graphics.DrawImage(img.Image, new Point(0, 0));*/ return ret_im; }
private static void SetAntiAlias(PhpGdImageResource img, Graphics g) { if (img.AntiAlias) { g.SmoothingMode = SmoothingMode.AntiAlias; } else { g.SmoothingMode = SmoothingMode.None; } }
/// <summary> /// Tries to create Pen most compatible to PHP drawing rules /// </summary> /// <param name="col"></param> /// <param name="img"></param> /// <param name="antiAliasable"></param> /// <returns></returns> private static Pen CreatePen(int col, PhpGdImageResource img, bool antiAliasable) { Pen pen; if (antiAliasable && img.AntiAlias) { if (col < 0) { pen = new Pen(Color.White); } else { pen = new Pen(PHPColorToNETColor(col)); } } else { if (col == (int)ColorValues.TILED) { if (img.tiled == null) { return new Pen(Color.Transparent); } else { pen = new Pen(img.tiled, img.LineThickness); } } else // IMG_STYLED if (col == -2) { if (img.styled == null) { return new Pen(Color.Transparent); } else { pen = new Pen(img.styled, img.LineThickness); } } // TODO: (Maros) Different than in PHP. And missing IMG_STYLED_BRUSHED. // IMG_BRUSHED else if (col == -3) { if (img.brushed == null) { return new Pen(Color.Transparent); } else { pen = new Pen(img.brushed, img.LineThickness); } } else { Color color = GetAlphaColor(img, col); pen = new Pen(color, img.LineThickness); } } return pen; }
public static PhpResource imagegrabscreen() { Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); PhpGdImageResource resource = new PhpGdImageResource(bmpScreenshot); using (Graphics g = Graphics.FromImage(resource.Image)) { try { g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); } catch { g.FillRectangle(new SolidBrush(Color.Black), 0, 0, resource.Image.Width, resource.Image.Height); } } return resource; }
public static PhpResource imagecreatetruecolor(int x_size, int y_size) { if (x_size <= 0 || y_size <= 0) { PhpException.Throw(PhpError.Warning, Utils.Resources.GetString("invalid_image_dimensions")); return null; } PhpGdImageResource img = new PhpGdImageResource(x_size, y_size); if (img == null) return null; // Draw black background using (Graphics g = Graphics.FromImage(img.Image)) { SolidBrush brush = new SolidBrush(Color.Black); g.FillRectangle(brush, 0, 0, img.Image.Width, img.Image.Height); brush.Dispose(); } img.AlphaBlending = true; return img; }
public static PhpResource imagecreatefromstring(PhpBytes image) { if (image == null) { PhpException.Throw(PhpError.Warning, Utils.Resources.GetString("empty_string_or_invalid_image")); return null; } PhpGdImageResource res; try { MemoryStream stream = new MemoryStream(image.ReadonlyData); Image img = Image.FromStream(stream, true, false); res = new PhpGdImageResource(img); } catch { PhpException.Throw(PhpError.Warning, Utils.Resources.GetString("empty_string_or_invalid_image")); return null; } return res; }
public static PhpResource imagecreate(int x_size, int y_size) { if (x_size <= 0 || y_size <= 0) { PhpException.Throw(PhpError.Warning, string.Format(Utils.Resources.GetString("invalid_image_dimensions"))); return null; } PhpGdImageResource img = new PhpGdImageResource(x_size, y_size); if (img == null) return null; // Draw white background using (Graphics g = Graphics.FromImage(img.Image)) { SolidBrush brush = new SolidBrush(Color.White); g.FillRectangle(brush, 0, 0, img.Image.Width, img.Image.Height); brush.Dispose(); } //TODO: (Maros) This function should create palette based image. // NOTE: (J) indexed image is created in Bitmap constructor, by providing PixelFormat.Indexed //img.IsTrueColor = false; return img; }