Tag: system.drawing

Bitmap.Save“对象目前正在其他地方使用”线程问题

我有一些像这样的代码: public void SaveImage(int Counter) { var task = Task.Factory.StartNew(() => { var image = FinalImage; if (image != null) { image.Save(FinalImageSaveLocation + “test” + Counter + “.bmp”); } }, TaskCreationOptions.PreferFairness); } 我有一个for循环使用类似的代码创建x数量的图像: for(int i = 0; i < 100; i++) { Pencil.DrawImage(image, x, y); //Pencil is created at a initialisation stage SaveImage(i); //by Pencil […]

处理静电刷

我正在写一个生物节律应用程序。 为了测试它,我有一个带有Button和PictureBox的表单。 当我点击按钮时,我做了 myPictureBox.Image = GetBiorhythm2(); 哪个第一次运行正常, 但在第二次单击时会导致以下exception: System.ArgumentException: Parameter is not valid. at System.Drawing.Graphics.CheckErrorStatus at System.Drawing.Graphics.FillEllipse at Larifari.Biorhythm.Biorhythm.GetBiorhythm2 in c:\delo\Horoskop\Biorhythm.cs:line 157 at Larifari.test.Button1Click in c:\delo\Horoskop\test.Designer.cs:line 169 at System.Windows.Forms.Control.OnClick at System.Windows.Forms.Button.OnClick at System.Windows.Forms.Button.OnMouseUp at System.Windows.Forms.Control.WmMouseUp at System.Windows.Forms.Control.WndProc at System.Windows.Forms.ButtonBase.WndProc at System.Windows.Forms.Button.WndProc at ControlNativeWindow.OnMessage at ControlNativeWindow.WndProc at System.Windows.Forms.NativeWindow.DebuggableCallback at ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop at ThreadContext.RunMessageLoopInner at ThreadContext.RunMessageLoop at System.Windows.Forms.Application.Run […]

ASP.NET Core中的服务器端图形

我最近将ASP.NET MVC应用程序从ASP.NET升级到ASP.NET Core。 在我的控制器操作中,我有一段依赖System.Drawing创建个人资料图片的代码 using (FileStream stream = new FileStream(HttpContext.Server.MapPath($”~/Content/UserFiles/{AuthenticatedUser.Id.ToString()}.jpg”), FileMode.OpenOrCreate)) { Image image = Image.FromStream(model.DisplayPicture.InputStream); image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); } 图像数据作为Base64编码图像发布到服务器 data:image/png;base64,…. 由于.NET Core中没有System.Drawing ,是否还有其他库可以实现这一目标?

用透明背景更改图像颜色

我需要使用c#(System.Drawings)将透明背景上带绿色圆圈的图像加载到位图图像中。 这很容易。 但是,我需要在将圆圈添加到更大的图像之前更改圆圈的颜色,而不会影响周围的透明度。 在我的情况下,我需要将圆形颜色更改为黄色并将其添加为太阳。 我不能使用固定的黄色圆圈图像,因为所需的颜色是动态的。 所以在下面的代码中,如何在将图像添加到位图之前更改图像的颜色? Image i = Image.FromFile(greenCircleFile); Bitmap b = new Bitmap(500, 500); using(Graphics g = Graphics.FromImage(b)) { //–> Here I need to change the color of the green circle to yellow //afterwards I can add it to the bitmap image g.DrawImage(i, 0, 0, 500, 500); } 请注意,需要考虑两件事:保持形状(圆圈)的抗锯齿,颜色需要由用户拾取并按原样使用以覆盖圆的原始颜色。 固定: 感谢@TaW,他提供了正确的答案。 然而,有一个小故障,这是最适合我的版本: Image […]

System.Drawing – 参数无效

经过一些更多的测试后,我发现这个问题可能是由于图像以某种方式没有被及时加载以克隆到位图并显示。 这是可能的还是没有? 注意:是的,标题中存在此错误还有其他问题,但从一些研究来看,它似乎是一个含糊不清的错误,有许多可能的原因。 我没有发现与我相同的情况有任何问题。 我收到以下错误。 System.ArgumentException was unhandled HResult=-2147024809 Message=Parameter is not valid. Source=System.Drawing 它起源于这段代码。 似乎是随机的(即,有时候它会起作用,有时却不起作用。连续运行的次数越多,没有重新启动VS并重建项目,失败的可能性就越大): private Bitmap GetSprite(bool anim, int tsIndex, int tileIdx) { System.Drawing.Rectangle cloneRect; string prefix = (anim) ? “A” : “S”; using (Bitmap b = new Bitmap(prefix + tsIndex.ToString() + “.png”)) { if (anim) { cloneRect = new System.Drawing.Rectangle(BaseObjects.A_AnimSpriteSets[tsIndex].StaticRecs[tileIdx].X, BaseObjects.A_AnimSpriteSets[tsIndex].StaticRecs[tileIdx].Y, BaseObjects.A_AnimSpriteSets[tsIndex].RecWidth, […]

如何在.NET / C#中的右键菜单上绘制?

我正在开发一个辅助技术应用程序(在C#中),它将信息叠加在当前打开的窗口之上。 它检测可点击的元素,并标记它们。 为此,我正在创建一个无边框透明窗口,TopMost设置为“true”,并在其上绘制标签。 这意味着在当前应用程序前面总会有一个窗口hover,我可以在其上绘制标签。 问题是,这个窗口不包括右键菜单 – 只包括其他窗口。 当用户右键单击时,将在叠加层上方绘制上下文菜单。 我需要能够在右键单击菜单中标记元素,但我无法在当前实现的基础上绘制它。 有人知道解决方案吗? 编辑:这是绘制叠加层的相关代码。 我在表单设计器中设置了表单选项,而不是代码明确,所以我不确定它会有多大帮助。 我删除了与绘图无关的代码或表单本身: public partial class OverlayForm : Form { public OverlayForm() { } protected override void OnPaint(PaintEventArgs eventArgs) { base.OnPaint(eventArgs); Graphics graphics = eventArgs.Graphics; Brush brush = new SolidBrush(this.labelColor); foreach (ClickableElement element in this.elements) { Region currentRegion = element.region; graphics.FillRegion(brush, currentRegion); } } }

System.Drawing.Image.FromStream()方法中的“参数无效”exception

我在网站上的Image.FromStream方法遇到了困难。 以下代码在我的计算机上运行良好。 但是当我将它上传到测试服务器时,它总是给我“参数无效”exception。 if (!afuImageFile.IsUploading && afuImageFile.HasFile) { System.Drawing.Image imgFile = System.Drawing.Image.FromStream(afuImageFile.FileContent); } afuImageFile是Ajax工具包中的AsynFileUploader控件。 afuImageFile.FileContent是一个HttpInputStream 。 我想我需要为某个文件夹添加一些权限。 谁能帮我?

从自定义PictureBox控件翻译不同大小的图像的点(X,Y)

我必须批量处理几个图像。我必须在特定点(X,Y)放置一些文本。有一个从图片框派生的自定义控件,允许用户拖动文本并将其放在所需的位置。 有两种类型的图像,我以不同的方式设置PictureBoxSizeMode 垂直图像 我设置> PictureBoxSizeMode.Zoom; 横 对于填充PictureBox的水平图像,我设置了> PictureBoxSizeMode.StretchImage 用户可以通过在该图片框控件上拖动文本来选择放置文本的位置。将原始图像调整为控件大小(对于水平图像),并且用户将文本拖动到该图像上。 基于Picturebox的SizeMode,使用以下代码将所选点转换为原始图像内的点 if (sizemode == 1) { transpoint = TranslateStretchImageMousePosition(new Point(eX – 20, eY -20)); } else if (sizemode == 2) { transpoint = TranslateZoomMousePosition(new Point(eX – 20, eY – 20)); } public Point TranslateStretchImageMousePosition(Point coordinates) { // test to make sure our image is not null […]

C#从运行时创建的文本框中获取文本

您好我正在制作一个包含2个文本框和2个按钮的程序当我按下添加按钮时,它将使用此代码创建2个新文本框: private void ADD_ROW_Click(object sender, EventArgs e) { //Make the NEW_TEXTBOX_1 HOW_FAR += 1; TextBox NEW_TEXTBOX_1 = new TextBox(); NEW_TEXTBOX_1.Name = “NAME_TEXTBOX_” + HOW_FAR.ToString(); //Set NEW_TEXTBOX_1 font NEW_TEXTBOX_1.Font = new Font(“Segoe Print”, 9); NEW_TEXTBOX_1.Font = new Font(NEW_TEXTBOX_1.Font, FontStyle.Bold); //Set pos and size and then create it. NEW_TEXTBOX_1.Location = new System.Drawing.Point(16, 71 + (35 * HOW_FAR)); […]

如何调整以编程方式创建的位图的大小来匹配在其上绘制的文本?

我有以下.ashx页面,它接受一些查询字符串参数并返回一个位图,上面写有指定的文本。 我遇到的问题是,我目前只是手动设置位图的初始大小为100 X 100,而我真正想要的是让位图足够大以包含写入它的所有文本。 我怎样才能做到这一点? public void ProcessRequest (HttpContext context) { context.Response.ContentType = “image/png”; string text = context.Request.QueryString[“Text”]; //set FontName string fontName; if (context.Request.QueryString[“FontName”] != null) { fontName = context.Request.QueryString[“FontName”]; } else { fontName = “Arial”; } //Set FontSize int fontEms; if (context.Request.QueryString[“FontSize”] != null) { string fontSize = context.Request.QueryString[“FontSize”]; fontEms = Int32.Parse(fontSize); } else […]