system.drawing

Change image color with transparent background

好久不见. 提交于 2019-12-05 14:05:56
I need to load an image with green circle over a transparent background into a bitmap image using c# (System.Drawings). That's the easy part. However I need to change the color of the circle before adding it to the bigger image, without affecting the transparency of the surrounding. In my case I need to change the circle color to yellow and add it as a sun. I can't use fixed yellow circle image because the desired color is dynamic. So in the code below, how can I change the color of the image before adding it to the bitmap? Image i = Image.FromFile(greenCircleFile); Bitmap b = new Bitmap(500,

System.Drawing.Icon constructor throwing “Operation completed successfully” exception

白昼怎懂夜的黑 提交于 2019-12-05 10:19:17
On a Windows XP machine, the following code is throwing a System.ComponentModel.Win32Exception with the message "The operation completed successfully" System.Drawing.Icon icon = new System.Drawing.Icon("icon.ico"); I can stop the program crashing with try { System.Drawing.Icon icon = new System.Drawing.Icon("icon.ico"); } catch(System.ComponentModel.Win32Exception ex) { if (ex.NativeErrorCode != 0) { throw; } } but of course the icon is not set. The full stack trace is at System.Drawing.Icon.Initialize(Int32 width, Int32 height) at System.Drawing.Icon..ctor(String fileName, Int32 width, Int32

DrawToBitmap - System.ArgumentException: Parameter is not valid

a 夏天 提交于 2019-12-05 08:31:33
Im creating a Label and sometimes Im using .DrawToBitmap() . I dont know why, but after Im running my program for a while (and calling .DrawToBitmap() often) I get the exception: System.ArgumentException: Parameter is not valid. at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format) at System.Drawing.Bitmap..ctor(Int32 width, Int32 height) Somehow I cannot call this function that often. If I would radically try this: while(true) { System.Windows.Forms.Label label = new Label(); label.Font = new Font("Arial", 20); label.Text = "test"; try { Bitmap image = new Bitmap(300,

Get System.Drawing.Font width?

孤街醉人 提交于 2019-12-05 08:30:13
I'm using .Net tools to do some 2D drawing. System.Drawing.Font uses a GetHeight() that returns the height in pixels. I'm missing a GetWidth() to retrieve the width! What should I use? Use Graphics.MeasureString Method (String, Font) : Eg. // Set up string. string measureString = "Measure String"; Font stringFont = new Font("Arial", 16); // Measure string. SizeF stringSize = new SizeF(); stringSize = e.Graphics.MeasureString(measureString, stringFont); // This will give you string width, from which you can calculate further double width = stringSize.Width What width? GetHeight returns the

How can I make label text scale-able in Winforms application

和自甴很熟 提交于 2019-12-05 07:03:56
I am looking for a way to make text in a label scale-able to it fit in entire parent container. The one way I can think of is to get the container size on window re-size and then increase or decrease font size accordingly, but that would limit its possibilities. Wondering if there is a better way of doing this, that may work more like an anchor property in Winforms application. I knew the answer is hidden somewhere in graphic object and paint event, playing around with these 2 keywords solved my problem. Here is the solution that worked in my particular case. I am simply changing the font size

.NET Image.Save method produces non - reproducible results on Windows 64 bit

痞子三分冷 提交于 2019-12-04 20:45:24
问题 I'm using .NET framework (tried 3.5 & 4.0) to load a .TIFF file and save it as .PNG. I expect two subsequent calls to the Save() method (using the same TIFF file) to produce the same PNG file. The produced files are, however, 'sometimes' different. The C# code below shows the problem: Image sourceToConvert = Bitmap.FromFile("c:\\tmp\\F1.tif"); sourceToConvert.Save("c:\\tmp\\F1_gen.png", ImageFormat.Png); for (int i = 0; i < 100; i++) { sourceToConvert = Bitmap.FromFile("c:\\tmp\\F1.tif");

Easy way to clean metadata from an image?

倖福魔咒の 提交于 2019-12-04 19:39:41
I'm working on a service for a company project that handles image processing, and one of the methods is supposed to clean the metadata from an image passed to it. I think implementation I currently have works, but I'm not sure if it's affecting the quality of images or if there's a better way to handle this task. Could you let me know if you know of a better way to do this? Here's the method in question: public byte[] CleanMetadata(byte[] data) { Image image; if (tryGetImageFromBytes(data, out image)) { Bitmap bitmap = new Bitmap(image); using (var graphics = Graphics.FromImage(bitmap)) {

How to avoid bitmap out of memory when working on very large image for ie: 10.000.000 pixel and above

痞子三分冷 提交于 2019-12-04 15:57:53
问题 Currently i'm working on a system that load a very large image, with minimum width x heigh >= 10.000.000 pixel. But the ratio of the user's upload image usually do not match our requirement ratio so i have to crop it to proper ratio, but when using System.Drawing bitmap to crop it, i always got SytemOutOfMemory exception. I have try Bitmap.Clone and Graphic.DrawImage with correct RectangleF but no luck. Is there anyways to do this without getting the outofmemory exception or are there any

Creating Visio look-a-like diagrams programmatically [closed]

徘徊边缘 提交于 2019-12-04 13:52:13
Closed. This question is off-topic. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 21 hours ago . I need to programmatically create Visio look-a-like diagrams as a drawing. Mostly we are going to create network layout diagrams similar to this one . Is there a component that could speed-up the development or some technique we could use? Generally we could do this on our own via System.Drawing, but this might get tricky for more complex network diagrams as we would have to ensure there are no

How to display a PictureBox from behind code in C#

落爺英雄遲暮 提交于 2019-12-04 10:25:57
I know my question sounds basic, but i searched all over the place and found nothing.. this is my code : public MainWindow() { InitializeComponent(); Map newMap = new Map(); newMap.setMapStrategy(new SmallMapStrategy()); newMap.createMap(); System.Windows.Forms.PictureBox pictureBox1 = new System.Windows.Forms.PictureBox(); pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(newMap.grid[3].afficher); } this is the afficher function : public override void afficher(object sender, PaintEventArgs e) { e.Graphics.DrawImage(squareImage, pos_x, pos_y, 50, 50); } squareImage is an