C
C#13mo ago
Cringel

❔ Trying to return

using System;
using System.Drawing;
using System.Windows.Forms;
using AForge.Imaging;
using AForge.Imaging.Filters;
using Image = System.Drawing.Image;

namespace Program_ui_test
{
public partial class Form1 : Form
{

private Bitmap savedImage;

public Form1()
{
InitializeComponent();

// Load the saved image you want to match
savedImage = (Bitmap)Image.FromFile("C:\\Users\\kingk\\RiderProjects\\Program ui test\\Program ui test\\Icon.png");
}

public FindMatchingImage()
{
// Capture the screen
Bitmap screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(screenshot);
g.CopyFromScreen(0, 0, 0, 0, screenshot.Size);

// Apply grayscale filter to both images
Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);
Bitmap processedSavedImage = grayscaleFilter.Apply(savedImage);
Bitmap processedScreenshot = grayscaleFilter.Apply(screenshot);

// Find the difference between the images
ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.8f);
TemplateMatch[] matches = tm.ProcessImage(processedScreenshot, processedSavedImage);

if (matches.Length > 0)
{
// Get the center coordinates of the matched region
Rectangle matchRect = matches[0].Rectangle;
int centerX = matchRect.X + matchRect.Width / 2;
int centerY = matchRect.Y + matchRect.Height / 2;

// Display the center coordinates
// MessageBox.Show($"Match found at ({centerX}, {centerY})");
Console.WriteLine($"Match found at ({centerX}, {centerY})");
return (centerX, centerY);
}
using System;
using System.Drawing;
using System.Windows.Forms;
using AForge.Imaging;
using AForge.Imaging.Filters;
using Image = System.Drawing.Image;

namespace Program_ui_test
{
public partial class Form1 : Form
{

private Bitmap savedImage;

public Form1()
{
InitializeComponent();

// Load the saved image you want to match
savedImage = (Bitmap)Image.FromFile("C:\\Users\\kingk\\RiderProjects\\Program ui test\\Program ui test\\Icon.png");
}

public FindMatchingImage()
{
// Capture the screen
Bitmap screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(screenshot);
g.CopyFromScreen(0, 0, 0, 0, screenshot.Size);

// Apply grayscale filter to both images
Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);
Bitmap processedSavedImage = grayscaleFilter.Apply(savedImage);
Bitmap processedScreenshot = grayscaleFilter.Apply(screenshot);

// Find the difference between the images
ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.8f);
TemplateMatch[] matches = tm.ProcessImage(processedScreenshot, processedSavedImage);

if (matches.Length > 0)
{
// Get the center coordinates of the matched region
Rectangle matchRect = matches[0].Rectangle;
int centerX = matchRect.X + matchRect.Width / 2;
int centerY = matchRect.Y + matchRect.Height / 2;

// Display the center coordinates
// MessageBox.Show($"Match found at ({centerX}, {centerY})");
Console.WriteLine($"Match found at ({centerX}, {centerY})");
return (centerX, centerY);
}
51 Replies
Cringel
CringelOP13mo ago
else
{
// MessageBox.Show("No match found.");
Console.WriteLine("No match found.");
return "No match found";
}
}

private void buttonFindImage_Click(object sender, EventArgs e)
{
FindMatchingImage();
}
}
}
else
{
// MessageBox.Show("No match found.");
Console.WriteLine("No match found.");
return "No match found";
}
}

private void buttonFindImage_Click(object sender, EventArgs e)
{
FindMatchingImage();
}
}
}
This is the code. Im trying to return (centerX, centerY) and "No match found"
Jimmacle
Jimmacle13mo ago
this doesn't even compile start from the top of your program, find the first error, figure out how to fix it, repeat until there are no more errors hint: this isn't python, everything needs to define a specific type
TheBoxyBear
TheBoxyBear13mo ago
Maybe compiles if the Designer file defines a savedImage member for some reason
Jimmacle
Jimmacle13mo ago
it 100% doesn't FindMatchingImage() has no return type, and if it did the 2 different paths try to return different types
TheBoxyBear
TheBoxyBear13mo ago
^ The current syntax is like a constructor for a non-existent class rather than a method
TheBoxyBear
TheBoxyBear13mo ago
Methods - C# Programming Guide - C#
A method in C# is a code block that contains a series of statements. A program runs the statements by calling the method and specifying arguments.
Angius
Angius13mo ago
From what I can see... 1. FindMatchingImage has no return type 2. You're trying to return a (int, int) tuple out of it at one point 3. And a string at one point
Cringel
CringelOP13mo ago
Hmm, I think I fixed it thanks
Angius
Angius13mo ago
Please tell me the solution wasn't something like giving the method a return type of object KEKW
Cringel
CringelOP13mo ago
Jimmacle
Jimmacle13mo ago
that's... a solution
Cringel
CringelOP13mo ago
works
Angius
Angius13mo ago
At least use a named tuple lmao
Cringel
CringelOP13mo ago
what is a named tuple?
Jimmacle
Jimmacle13mo ago
"works" and "good" are not the same, if all you want is what works then sure
Angius
Angius13mo ago
(int X, int Y, string message, bool success)
Jimmacle
Jimmacle13mo ago
based on your code it looks like you want a result type, try pattern, or maybe even just throw an exception
Cringel
CringelOP13mo ago
wuduhhell omagad what difference does it do?
Jimmacle
Jimmacle13mo ago
it names your tuple members
Cringel
CringelOP13mo ago
it tells only a variable can be a string?
Jimmacle
Jimmacle13mo ago
so you have X instead of Item1 for example
Cringel
CringelOP13mo ago
oh
Angius
Angius13mo ago
You can use theTuple.message instead of having to remember that the message is item 3 and using theTuple.Item3
Jimmacle
Jimmacle13mo ago
still really not the right solution imo if it was me i'd probably use the try pattern and leave the error message generation to code outside the method
Cringel
CringelOP13mo ago
Something is strange
No description
Cringel
CringelOP13mo ago
why it doesnt type to locals?
Jimmacle
Jimmacle13mo ago
?
Cringel
CringelOP13mo ago
No description
Jimmacle
Jimmacle13mo ago
what is "type to locals"
Cringel
CringelOP13mo ago
centerX and centerY are locals, according to lua
Jimmacle
Jimmacle13mo ago
this isn't lua so what does that mean in the context of C#
Cringel
CringelOP13mo ago
true but still I dont know any other name for them
Jimmacle
Jimmacle13mo ago
if i don't know what "type to locals" means i can't tell you why it's doing whatever that is
Cringel
CringelOP13mo ago
a variable
Jimmacle
Jimmacle13mo ago
not helping
Cringel
CringelOP13mo ago
No description
Cringel
CringelOP13mo ago
Int
Jimmacle
Jimmacle13mo ago
i see nothing wrong here, why are you confused? besides what i'm assuming is a NRT warning
Cringel
CringelOP13mo ago
Should have typed
No description
Jimmacle
Jimmacle13mo ago
so you want to see the inline hints?
Cringel
CringelOP13mo ago
A warning that is telling it may not exist
Jimmacle
Jimmacle13mo ago
that's an IDE feature, it's not part of your code and means nothing it's just to help you remember what the parameter name is
Cringel
CringelOP13mo ago
ok then
Jimmacle
Jimmacle13mo ago
if you want to see it all the time you'll have to configure that in your editor
Cringel
CringelOP13mo ago
nah, its ok
Jimmacle
Jimmacle13mo ago
but i will reiterate, this is a hacky solution and i don't recommend keeping it like this unless all you want is something that "works"
Cringel
CringelOP13mo ago
hacky means what? it means like, if I make a server with that code, my server can get hacked?
Jimmacle
Jimmacle13mo ago
no, like it's not a very elegant solution like i said, "code that works" and "code that is good" are different categories trust me, i inherited a ton of projects where they only cared about writing code that works and it's very difficult to work on
Cringel
CringelOP13mo ago
Its more like about making other people to be able to read the code tho but yeah, I get it
Jimmacle
Jimmacle13mo ago
"other people" includes you in a few weeks when you look at this code again 😛
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server