Tag: system.drawing

在C#中使用DrawString对文本进行对齐

我在System.Drawing.Graphics对象上绘制文本。 我正在使用DrawString方法,文本字符串, Font , Brush ,边界RectangleF和StringFormat作为参数。 看看StringFormat ,我发现我可以将它的Alignment属性设置为Near , Center或Far 。 但是我还没有找到将其设置为Justified的方法。 我怎样才能做到这一点? 谢谢您的帮助!

C#抽绳字母间距

在使用Graphics.DrawString时,是否可以以某种方式控制字母间距? 我找不到允许我这样做的DrawString或Font的任何重载。 g.DrawString(“MyString”, new Font(“Courier”, 44, GraphicsUnit.Pixel), Brushes.Black, new PointF(262, 638)); 字母间距我指的是字母之间的距离。 如果我添加了足够的空间,间距MyString可能看起来像是什么。

在Winforms中绘制一条线

我在一个简单的窗体中在组框中绘制一条线时遇到问题。 这是我的代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); DrawLShapeLine(groupBox1.CreateGraphics(), 10, 10, 20, 40); } public void DrawLShapeLine(System.Drawing.Graphics g, int intMarginLeft, int intMarginTop, int intWidth, int intHeight) { Pen myPen = new Pen(Color.Black); myPen.Width = 2; // Create array of points that define […]

如何在C#/ WPF / WinForms中将WMF渲染到BitMap时启用消除锯齿function?

这样做时为什么线路等不会被消除锯齿? using (var myGraphics = Graphics.FromImage(bitmap)) { myGraphics.CompositingQuality = CompositingQuality.HighQuality; myGraphics.SmoothingMode = SmoothingMode.HighQuality; myGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; myGraphics.Clear(backgroundColor); myGraphics.EnumerateMetafile(m_metafile, new Point(0, 0), m_metafileDelegate); } 委托函数如下所示: private bool MetafileCallback(EmfPlusRecordType recordType, int flags, int dataSize, IntPtr data, PlayRecordCallback callbackData) { byte[] dataArray = null; if (data != IntPtr.Zero) { // Copy the unmanaged record to a managed byte […]

图形对象到图像文件

我想裁剪并调整我的图像大小。 这是我的代码: 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); 现在我假设我生成的裁剪/resize的图像存储在图形对象中。 问题是 – 如何将其保存到文件中?

使用LockBits从内存创建的GDI +generics错误保存位图

根据我在SO和网络上的研究,保存位图时的GDI +generics错误显然是一个常见问题。 鉴于以下简化代码段: 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); […]

将文件加载到位图但保持原始文件不变

如何在C#中做到这一点? 如果我使用Bitmap.FromFile(),原始文件将被锁定。 如果我使用Bitmap.FromStream(),原始文件不会被锁定,但文档说“你必须在图像的生命周期内保持流打开”。 这可能意味着文件仍然链接到图像对象(例如,如果文件更改,则对象反之亦然)。 我想要做的只是读取位图并将其保存到一个对象,然后在文件和Image对象之间没有任何链接

C#图形闪烁

我正在研究一种绘图程序,但是在绘制橡皮带线时移动鼠标光标时出现闪烁问题。 我希望你可以帮我删除那条闪烁的行,这里是代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace GraphicsTest { public partial class Form1 : Form { int xFirst, yFirst; Bitmap bm = new Bitmap(1000, 1000); Graphics bmG; Graphics xG; Pen pen = new Pen(Color.Black, 1); bool draw = false; public Form1() { InitializeComponent(); […]

如何迭代.gif图像中的每个像素?

我需要逐步浏览.gif图像并确定每个像素的RGB值,x和y坐标。 有人能给我一个如何实现这个目标的概述吗? (方法,要使用的命名空间等)

使用Graphics.DrawString时动态调整字体大小以适应空间

有没有人有提示,而你可以动态调整字体大小以适应特定区域? 例如,我有一个800×110的矩形,我想用最大尺寸字体填充它,这将支持我试图显示的整个字符串。 Bitmap bitmap = new Bitmap(800, 110); using (Graphics graphics = Graphics.FromImage(bitmap)) using (Font font1 = new Font(“Arial”, 120, FontStyle.Regular, GraphicsUnit.Pixel)) { Rectangle rect1 = new Rectangle(0, 0, 800, 110); StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; graphics.DrawString(“Billy Reallylonglastnameinstein”, font1, Brushes.Red, rect1, stringFormat); } bitmap.Save(Server.MapPath(“~/Fonts/” + System.Guid.NewGuid() […]