C# 用c语言将图像转换为图形#

C# 用c语言将图像转换为图形#,c#,C#,如何将图像转换为图形?您需要一个图像才能在上面绘制图形,因此您可能已经拥有该图像: Graphics g = Graphics.FromImage(image); 如果直接在控件的图形上绘图,则可以创建与控件尺寸相同的新位图,然后调用Control.DrawToBitmap()。但是,更好的方法通常是从位图开始,绘制其图形(如Darin所建议的),然后将位图绘制到控件上。您不能将图形对象转换为图像,因为图形对象不包含任何图像数据 Graphics对象只是一个用于在画布上绘制的工具。该画布通常是

如何将图像转换为图形?

您需要一个图像才能在上面绘制图形,因此您可能已经拥有该图像:

Graphics g = Graphics.FromImage(image);

如果直接在控件的图形上绘图,则可以创建与控件尺寸相同的新位图,然后调用Control.DrawToBitmap()。但是,更好的方法通常是从位图开始,绘制其图形(如Darin所建议的),然后将位图绘制到控件上。

您不能将
图形对象转换为图像,因为
图形对象不包含任何图像数据

Graphics
对象只是一个用于在画布上绘制的工具。该画布通常是
位图
对象或屏幕

如果
图形
对象用于在
位图
上绘制,则您已经拥有该图像。如果
图形
对象用于在屏幕上绘制,则必须进行屏幕截图以获得画布的图像


如果
Graphics
对象是从窗口控件创建的,则可以使用控件的
DrawToBitmap
方法在图像上而不是在屏幕上渲染控件。

如Darin所述,您可能已经拥有该图像。如果没有,您可以创建一个新的并绘制到那个

Image bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp)) {
    // draw in bmp using g
}
bmp.Save(filename);

将图像保存到硬盘上的文件中。

将图形转换为位图的最佳方法是删除“正在使用”的内容:

        Bitmap b1 = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);
        Graphics g = Graphics.FromImage(b1);
        g.CopyFromScreen(0, 0, Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
        b1.Save("screen.bmp");
我是在研究如何将图形转换为位图时发现这一点的,它就像一个符咒

我有一些关于如何使用此功能的示例:

    //1. Take a screenshot
   Bitmap b1 = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);
    Graphics g = Graphics.FromImage(b1);
    g.CopyFromScreen(0, 0, Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
    b1.Save("screen.bmp");

   //2. Create pixels (stars) at a custom resolution, changing constantly like stars

    private void timer1_Tick(object sender, EventArgs e)
    {
        /*
        * Steps to use this code:
        * 1. Create new form
        * 2. Set form properties to match the settings below:
        *       AutoSize = true
        *       AutoSizeMode = GrowAndShrink
        *       MaximizeBox = false
        *       MinimizeBox = false
        *       ShowIcon = false;
        *       
        * 3. Create picture box with these properties:
        *       Dock = Fill
        * 
        */

        //<Definitions>
        Size imageSize = new Size(400, 400);
        int minimumStars = 600;
        int maximumStars = 800;
        //</Definitions>

        Random r = new Random();
        Bitmap b1 = new Bitmap(imageSize.Width, imageSize.Height);
        Graphics g = Graphics.FromImage(b1);
        g.Clear(Color.Black);
        for (int i = 0; i <r.Next(minimumStars, maximumStars); i++)
        {
            int x = r.Next(1, imageSize.Width);
            int y = r.Next(1, imageSize.Height);
            b1.SetPixel(x, y, Color.WhiteSmoke);
        }
        pictureBox1.Image = b1;

    }
//1。截图
位图b1=新位图(Screen.PrimaryScreen.Bounds.Width、Screen.PrimaryScreen.Bounds.Height);
图形g=图形。来自图像(b1);
g、 CopyFromScreen(0,0,Screen.PrimaryScreen.Bounds.X,Screen.PrimaryScreen.Bounds.Y,新大小(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height));
b1.保存(“screen.bmp”);
//2. 以自定义分辨率创建像素(星星),像星星一样不断变化
私有无效计时器1_刻度(对象发送方,事件参数e)
{
/*
*使用此代码的步骤:
*1.创建新表单
*2.设置表单属性以匹配以下设置:
*自动调整大小=真
*AutoSizeMode=增长和收缩
*最大化ebox=false
*b x=false
*ShowIcon=false;
*       
*3.使用以下属性创建图片框:
*码头=填充
* 
*/
//
尺寸imageSize=新尺寸(400400);
int最小星数=600;
int最大星星数=800;
//
随机r=新随机();
位图b1=新位图(imageSize.Width、imageSize.Height);
图形g=图形。来自图像(b1);
g、 清晰(颜色为黑色);

对于(int i=0;i@Sorush:i)我修复了它。(为了将来的参考,如果你打算发表评论以便Hesam得到通知,你应该对问题发表评论,而不是回答。)这是问题的相反情况:问题是:图形对图像,而不是你回答的图像对图像