system.drawing

Graphics.DrawImage unexpectedly resizing image

痴心易碎 提交于 2019-11-29 10:26:35
I've got some code that takes a png that is a greyscale image with transparency and attempts to create a new image that has a given background colour (looked up from a database) and overlays the original image on it to create an image of the required colour with highlights and shading. The code is run in an ASP.NET context but I don't think that is relevant. The code works fine on my local computer but when it is deployed to our UAT server it gives unexpected results. On the UAT it creates an image of the right size but the shaded/highlighted area seems to have been shrunk in each dimension by

Graphics object to image file

时光怂恿深爱的人放手 提交于 2019-11-29 09:52:26
I would like to crop and resize my image. Here is my code: Image image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Cropper/tests/castle.jpg"); // Crop and resize the image. Rectangle destination = new Rectangle(0, 0, 200, 120); Graphics graphic = Graphics.FromImage(image); graphic.DrawImage(image, destination, int.Parse(X1.Value), int.Parse(Y1.Value), int.Parse(Width.Value), int.Parse(Height.Value), GraphicsUnit.Pixel); Now I assume that my resulting cropped/resized image is stored in the graphics object. The question is - how do I save it to a file? The Graphics object that you

Silverlight 4.0: How to convert byte[] to image?

China☆狼群 提交于 2019-11-29 06:21:08
问题 public Image Base64ToImage(string base64String) { // Convert Base64 String to byte[] byte[] imageBytes = Convert.FromBase64String(base64String); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); // Convert byte[] to Image ms.Write(imageBytes, 0, imageBytes.Length); System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true); return image; } I want to convert byte[] to image, however System.Drawing.Image is not supported in Silverlight. Any alternative? 回答1: You

How do you Draw Transparent Image using System.Drawing?

本秂侑毒 提交于 2019-11-29 06:14:58
I'm trying to return a transparent GIF from an .aspx page for display within a web page. I am trying to get the image to have transparency, but I just keep getting Black being where the image should be Transparent. Does anyone know what I'm doing wrong? Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Me.Load '' Change the response headers to output a GIF image. Response.Clear() Response.ContentType = "image/gif" Dim width = 110 Dim height = width '' Create a new 32-bit bitmap image Dim b = New Bitmap(width, height) '' Create Grahpics object for drawing

Convert System.Drawing.Color to System.Windows.Media.Color [duplicate]

橙三吉。 提交于 2019-11-28 22:44:48
This question already has an answer here: How to convert from System.Drawing.Color to System.Windows.Media.Color? 3 answers System.Drawing.Color drawRedColor = System.Drawing.Color.Red; System.Windows.Media.Color mediaColor = ?drawRedColor.ToMediaColor();? How about: using MColor = System.Windows.Media.Color; using DColor = System.Drawing.Color; ... public static MColor ToMediaColor(this DColor color) { return MColor.FromArgb(color.A, color.R, color.G, color.B); } EDIT: Fixed the 'unpacking' of the ARGB. System.Windows.Media.Color mediaColor = System.Windows.Media.Color.FromRgb(Color.Red.R,

Why is DarkGray lighter than Gray?

给你一囗甜甜゛ 提交于 2019-11-28 16:05:01
问题 Simple curiosity here, tinged with some practical concerns because I get caught out by this occasionally. How come Color.DarkGray is lighter than Color.Gray? 回答1: Wikipedia has some information on the subject. It sounds like a lot of the named color definitions come from X Windows System. On X, "Gray" is actually closer to "Silver". However, the W3C defined Gray (more appropriately?) as RGB 50%. Here's some more Wikipedia on the subject: Perhaps most unusual of the color clashes between X11

GDI+ generic error saving bitmap created from memory using LockBits

独自空忆成欢 提交于 2019-11-28 12:10:31
The GDI+ generic error when saving a bitmap is obviously a common problem according to my research here on SO and the web. Given following simplified snippet: byte[] bytes = new byte[2048 * 2048 * 2]; for (int i = 0; i < bytes.Length; i++) { // set random or constant pixel data, whatever you want } Bitmap bmp = new Bitmap(2048, 2048, PixelFormat.Format16bppGrayScale); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, 2048, 2048), ImageLockMode.ReadWrite, bmp.PixelFormat); System.Runtime.InteropServices.Marshal.Copy(bytes, 0, bmpData.Scan0, 8388608); bmp.UnlockBits(bmpData); bmp.Save(@"name

PDF Convert to Black And White PNGs

百般思念 提交于 2019-11-28 11:45:51
I'm trying to compress PDFs using iTextSharp. There are a lot of pages with color images stored as JPEGs (DCTDECODE)...so I'm converting them to black and white PNGs and replacing them in the document (the PNG is much smaller than a JPG for black and white format) I have the following methods: private static bool TryCompressPdfImages(PdfReader reader) { try { int n = reader.XrefSize; for (int i = 0; i < n; i++) { PdfObject obj = reader.GetPdfObject(i); if (obj == null || !obj.IsStream()) { continue; } var dict = (PdfDictionary)PdfReader.GetPdfObject(obj); var subType = (PdfName)PdfReader

OutOfMemoryException: Out of memory - System.Drawing.Graphics.FromImage

旧街凉风 提交于 2019-11-28 10:03:21
I get Out of Memory exception when using System.Drawing.Graphics.FromImage (using latest versions of .NET software on Windows 2012 server), ONLY on a very few specific image files . Most of the time the code works fine. Typical answers to above issue indicate that certain resources are not being released. Please consider the following before answering:- This specific image is 34KB in size, is a .JPG image. Server is idle and has over 32GB RAM. If I look at properties of this jpg file, using windows explorer, by right-clicking on file, Windows says: 96 dpi and 32 bit depth . BUT, if I open this

Justifying text using DrawString in C#

我怕爱的太早我们不能终老 提交于 2019-11-28 08:40:15
问题 I'm drawing text on a System.Drawing.Graphics object. I'm using the DrawString method, with the text string, a Font , a Brush , a bounding RectangleF , and a StringFormat as arguments. Looking into StringFormat , I've found that I can set it's Alignment property to Near , Center or Far . However I haven't found a way to set it to Justified. How can I achieve this? Thank you for your help! 回答1: There is no built-in way to do it. Some work-arounds are mentioned on this thread: http://social