任何人都有一个很好的例子,说明如何将文本写入jpg图像并使用.NET中的System.Drawing重新保存它?
我尝试使用以下代码直接绘制到屏幕:
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr ptr);
static void draw(Rectangle r, Brush b, IntPtr hwnd)
{
using (Graphics g = Graphics.FromHwnd(hwnd))
{
g.FillRectangle(b, r);
}
}
static void Main(string[] args)
{
draw(new Rectangle(0, 0, 400, 400), Brushes.PaleGoldenrod, GetDC(IntPtr.Zero));
}
Run Code Online (Sandbox Code Playgroud)
查阅文档和各种示例,这应该是有效的代码。尽管如此,我在以下行收到 OutOfMemoryException:
using(Graphics g = Graphics.FromHwnd(hwnd))
Run Code Online (Sandbox Code Playgroud)
由于我只查询单个句柄,因此我不明白如何引发此异常。此示例中没有其他代码。
我想在C#windows Form Application上绘制YinYang符号.到目前为止,我画了一个大的外圈和两个内在的圈子.
我需要帮助绘制沿圆圈中间延伸的曲线部分
另外,我如何填充小圆圈和圆圈的另一半为黑色.
此外,无需使用按钮即可绘制(请参阅代码).
这是我的代码片段:
private void button1_Click(object sender, EventArgs e)
{
Graphics myGraphics = base.CreateGraphics();
Pen myPen = new Pen(Color.Black);
SolidBrush mySolidBrush = new SolidBrush(Color.Black);
myGraphics.DrawEllipse(myPen, 50,50, 150, 150);
Graphics innerCircle = base.CreateGraphics();
Pen myPen2 = new Pen(Color.Black);
SolidBrush mySolidBrush2 = new SolidBrush(Color.Black);
myGraphics.DrawEllipse(myPen, 118, 75, 20, 20);
Graphics innerCircle2 = base.CreateGraphics();
Pen myPen3 = new Pen(Color.Black);
SolidBrush mySolidBrush3 = new SolidBrush(Color.Black);
myGraphics.DrawEllipse(myPen, 118, 150, 20, 20);
}
Run Code Online (Sandbox Code Playgroud) 我把这个图像表示为base64字符串,我已粘贴在这里:https: //paste.ubuntu.com/23343680/ 里面有一个图像.我打电话给
Convert.FromBase64String
Run Code Online (Sandbox Code Playgroud)
它给了我
{"Invalid length for a Base-64 char array or string."}
Run Code Online (Sandbox Code Playgroud)
现在我使用这个网站http://codebeautify.org/base64-to-image-converter来粘贴相同的字符串,它可以很好地渲染数据中的图像.我在这里做错了什么,我需要得到一个字节[],我将从中制作图像,但我不能.谢谢.
所以我有一个按一些标准订购的点数列表:
List<System.Drawing.Point> points = new List<System.Drawing.Point>();
System.Drawing.Point prev = new System.Drawing.Point();
Run Code Online (Sandbox Code Playgroud)
我正在使用该列表中最接近的2个点之间绘制线条
prev = points[0];
System.Diagnostics.Stopwatch s1 = System.Diagnostics.Stopwatch.StartNew();
for (int i = 1; i < points.Count; i++)
{
var pp = points[i];
using (Graphics dr = Graphics.FromImage(img))
{
dr.DrawLine(bluePen, prev.X, prev.Y, pp.X, pp.Y);
prev.X = pp.X;
prev.Y = pp.Y;
}
}
s1.Stop();
Run Code Online (Sandbox Code Playgroud)
对于908(宽度)x297(高度)像素图像,这段代码需要2-4秒.
我该怎么做才能提高速度?
编辑:发布下面的最终结果.虽然第一种方法仍然允许更精细地操纵图纸.
using (Graphics dr = Graphics.FromImage(img))
dr.DrawLines(bluePen, points.ToArray());
Run Code Online (Sandbox Code Playgroud) 我们正在构建一个使用 System.Drawing 包的 .Net Core 2.0 C# Web 应用程序。
我在Mac上编写代码,但它也会在Windows机器上开发,并部署到Linux服务器上。
要在 OSX 上使用 System.Drawing,我必须将 runtime.osx.10.10-x64.CoreCompat.System.Drawing Nuget 包添加到我的项目依赖项中。
但该代码现在无法在 Windows 机器上运行。当尝试使用 Graphics(System.Drawing 的一部分)对象的 DrawString 方法时,我们得到 System.AccessViolationException。
所以,我的问题是解决这个问题的常用方法是什么?是否可以在这个多操作系统设置中使用 System.Drawing 类?
我想对黑白图像进行着色。
为此,我使用 ColorMatrix。例如,当我想将图像着色为红色时,我使用以下 ColorMatrix:
1,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0,
1,0,0,0,0,
Run Code Online (Sandbox Code Playgroud)
现在我想从比色图表中选择一种颜色,例如从这样的比色图表中选择:
我的计划是使用 GetPixel 检索光标下的颜色并将该 RGB 值转换为 ColorMatrix。
当我在红色上使用 GetPixel 时,我会得到
R: 255
G: 0
B: 0
Run Code Online (Sandbox Code Playgroud)
如何将 255 的红色值转换为 ColorMatrix 值?它将是1.0。如果红色值为 127,则该值为 0.5
我应该简单地使用“三规则”来确定 1.0 的值,还是应该如何处理?
我正在尝试将此代码从Java转换为C#(位于此处)
我有一些winform经验,但在winform应用程序上绘制像素时经验不足。
我很自信我可以转换大多数子方法,但不清楚如何在屏幕上绘制单个像素
将Java转换为C#的任何帮助或工具将不胜感激
// Buddhabrot
// j.tarbell January, 2004
// Albuquerque, New Mexico
// complexification.net
// based on code by Paul Bourke
// astronomy.swin.edu.au/~pbourke/
// Processing 0085 Beta syntax update
// j.tarbell April, 2005
int dim = 800; // screen dimensions (square window)
int bailout = 200; // number of iterations before bail
int plots = 10000; // number of plots to execute per frame (x30 = plots per second)
// 2D array to hold exposure values …Run Code Online (Sandbox Code Playgroud) 我正在为我的comp103文件做一些修改,我无法使用Graphics.Clear()获取按钮; 清除图形纸的方法.有人可以指出我的代码中的错误,因为我花了大约一个小时试图在网上找到答案.
很抱歉问这样一个菜鸟问题.
这是代码:
namespace Week_4_Ex1
{
public partial class Form1 : Form
{
const int height1 = 150; // A constant that stores the flags height
const int width1 = 100; // A constant that stores the flags width
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Button event that Draws a flag
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDraw_Click(object sender, EventArgs e)
{
Graphics Paper = this.CreateGraphics(); // Creates graphics paper to draw on …Run Code Online (Sandbox Code Playgroud) c# ×11
system.drawing ×11
.net ×2
winforms ×2
.net-core ×1
asp.net ×1
colormatrix ×1
gdi+ ×1
math ×1
processing ×1
quadratic ×1
vb.net ×1