如何在c#?noobie中将Windows窗体应用程序更改为控制台应用程序 [英] how can I change a windows form application to console application in c#?noobie

查看:107
本文介绍了如何在c#?noobie中将Windows窗体应用程序更改为控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何正确应用以下项目的源代码作为控制台应用程序?



[查找包含在另一个位图中的位图]

我到目前为止这样做了:







 使用系统; 
使用 System.Threading;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Drawing.Imaging;
使用 System.Diagnostics;


命名空间 BitmapDetector
{
class 程序
{
静态 void Main( string [] args)
{
string result = searchBitmap(arg [ 0 ],arg [ 1 ],arg [ 2 ]);
Console.WriteLine(result);
}


private 矩形searchBitmap(Bitmap smallBmp,Bitmap bigBmp, double tolerance)
{
BitmapData smallData =
smallBmp.LockBits( new Rectangle( 0 0 ,smallBmp.Width,smallBmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
BitmapData bigData =
bigBmp.LockBits( new 矩形( 0 0 ,bigBmp.Width,bigBmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format24bppRgb );

int smallStride = smallData.Stride;
int bigStride = bigData.Stride;

int bigWidth = bigBmp.Width;
int bigHeight = bigBmp.Height - smallBmp.Height + 1 ;
int smallWidth = smallBmp.Width * 3 ;
int smallHeight = smallBmp.Height;

Rectangle location = Rectangle.Empty;
int margin = Convert.ToInt32( 255 0 *容差);

不安全
{
byte * pSmall =(byte *)(void *)smallData.Scan0;
byte * pBig =(byte *)(void *)bigData.Scan0;

int smallOffset = smallStride - smallBmp.Width * 3 ;
int bigOffset = bigStride - bigBmp.Width * 3 ;

bool matchFound = true ;

for int y = 0 ; y < bigHeight; y ++)
{
for int x = 0 ; x < bigWidth; x ++)
{
byte * pBigBackup = pBig;
byte * pSmallBackup = pSmall;

// 寻找小图片。
for int i = 0 ; i < smallHeight; i ++)
{
int j = 0 ;
matchFound = true ;
for (j = 0 ; j < ; smallWidth; j ++)
{
// 带公差:pSmall值应该介于边距之间。
int inf = pBig [ 0 ] - 保证金;
int sup = pBig [ 0 ] +保证金;
if (sup < pSmall [ 0 ] || inf > pSmall [ 0 ])
{
matchFound = false ;
break ;
}

pBig ++;
pSmall ++;
}

如果(!matchFound) break ;

// 我们恢复指针。
pSmall = pSmallBackup ;
pBig = pBigBackup;

// 小图片和大图片的下一行。
pSmall + = smallStride *( 1 + i);
pBig + = bigStride *( 1 + i);
}

// 如果找到匹配项,我们会返回。
if (matchFound)
{
location.X = x;
location.Y = y;
location.Width = smallBmp.Width;
location.Height = smallBmp.Height;
break ;
}
// 如果找不到匹配项,我们会恢复指针并继续。
else
{
pBig = pBigBackup;
pSmall = pSmallBackup;
pBig + = 3 ;
}
}

if (matchFound) break ;

pBig + = bigOffset;
}
}

bigBmp.UnlockBits(bigData);
smallBmp.UnlockBits(smallData);

返回位置;
}
}
}

解决方案

您只需要添加一个控制台。这是通过Project =>更改程序集属性来完成的。属性(应用程序选项卡)=>输出类型为控制台应用程序。

此选项违反直觉。如果你这样做,你的应用程序将不会抓住一个 System.Windows.Forms 应用程序。相反,它只会使控制台可见;并且该控制台中将显示 System.Console.Write System.Console.WriteLine 的输出控制台I / O的其他功能将可用。至于你的表格,你甚至可以将它们保留原样或稍后删除。



这就是全部。







命令行参数与控制台或无控制台应用程序无关。它们适用于各种应用。我在文章中解释了如何获取它们,我还提供了一个易于使用的可靠实用程序来解析它们: 基于枚举的命令行实用程序



-SA

Hi, how can I apply the following project's source code properly to be a console app?

[Finding a Bitmap contained inside another Bitmap]
I did this so far:



using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;
using System.Diagnostics;


namespace BitmapDetector
{
    class Program
    {
        static void Main(string[] args)
        {
           string result = searchBitmap(arg[0], arg[1], arg[2]);
            Console.WriteLine(result);
        }


        private Rectangle searchBitmap(Bitmap smallBmp, Bitmap bigBmp, double tolerance)
        {
            BitmapData smallData =
              smallBmp.LockBits(new Rectangle(0, 0, smallBmp.Width, smallBmp.Height),
                       System.Drawing.Imaging.ImageLockMode.ReadOnly,
                       System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            BitmapData bigData =
              bigBmp.LockBits(new Rectangle(0, 0, bigBmp.Width, bigBmp.Height),
                       System.Drawing.Imaging.ImageLockMode.ReadOnly,
                       System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            int smallStride = smallData.Stride;
            int bigStride = bigData.Stride;

            int bigWidth = bigBmp.Width;
            int bigHeight = bigBmp.Height - smallBmp.Height + 1;
            int smallWidth = smallBmp.Width * 3;
            int smallHeight = smallBmp.Height;

            Rectangle location = Rectangle.Empty;
            int margin = Convert.ToInt32(255.0 * tolerance);

            unsafe
            {
                byte* pSmall = (byte*)(void*)smallData.Scan0;
                byte* pBig = (byte*)(void*)bigData.Scan0;

                int smallOffset = smallStride - smallBmp.Width * 3;
                int bigOffset = bigStride - bigBmp.Width * 3;

                bool matchFound = true;

                for (int y = 0; y < bigHeight; y++)
                {
                    for (int x = 0; x < bigWidth; x++)
                    {
                        byte* pBigBackup = pBig;
                        byte* pSmallBackup = pSmall;

                        //Look for the small picture.
                        for (int i = 0; i < smallHeight; i++)
                        {
                            int j = 0;
                            matchFound = true;
                            for (j = 0; j < smallWidth; j++)
                            {
                                //With tolerance: pSmall value should be between margins.
                                int inf = pBig[0] - margin;
                                int sup = pBig[0] + margin;
                                if (sup < pSmall[0] || inf > pSmall[0])
                                {
                                    matchFound = false;
                                    break;
                                }

                                pBig++;
                                pSmall++;
                            }

                            if (!matchFound) break;

                            //We restore the pointers.
                            pSmall = pSmallBackup;
                            pBig = pBigBackup;

                            //Next rows of the small and big pictures.
                            pSmall += smallStride * (1 + i);
                            pBig += bigStride * (1 + i);
                        }

                        //If match found, we return.
                        if (matchFound)
                        {
                            location.X = x;
                            location.Y = y;
                            location.Width = smallBmp.Width;
                            location.Height = smallBmp.Height;
                            break;
                        }
                        //If no match found, we restore the pointers and continue.
                        else
                        {
                            pBig = pBigBackup;
                            pSmall = pSmallBackup;
                            pBig += 3;
                        }
                    }

                    if (matchFound) break;

                    pBig += bigOffset;
                }
            }

            bigBmp.UnlockBits(bigData);
            smallBmp.UnlockBits(smallData);

            return location;
        }
    }
}

解决方案

You just need to add a console. This is done by changing the Assembly properties via Project => Properties ("Application" tab) => Output Type to "Console Application".
This option is counter-intuitive. If you do it, your application won't seize to be a System.Windows.Forms application. Instead, it will just make the console visible; and the output of System.Console.Write or System.Console.WriteLine will be shown in that console, and all other features of console I/O will became available. As to your forms, you can even leave them as is or remove later.

That's all.

[EDIT]

Command-line arguments have nothing to do with console or no-console applications. They are applicable to all kinds of applications. I explain how to get them in my article where I also provide an easy-to-use reliable utility for parsing them: Enumeration-based Command Line Utility.

—SA


这篇关于如何在c#?noobie中将Windows窗体应用程序更改为控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆