Bot Detect Captcha Set multiple image styles - c#

Is there a way to set multiple image style for bot detect captcha?
I am referring to this sample.
I have written an extension class to initialize captcha,
public class CaptchaHelper
{
public static MvcCaptcha GetTicketsCaptcha()
{
// create the control instance
MvcCaptcha myCaptcha = new MvcCaptcha("ExampleCaptcha");
myCaptchaImageStyle = BotDetect.ImageStyle.AncientMosaic;
}
}
I can set one style, but cannot find a way to set multiple options. Need some help?

There is a way to do this, "CaptchaRandomization.GetRandomImageStyle" accepts ImageStyle array.
ImageStyle[] allowedStyles =
{
ImageStyle.AncientMosaic,
ImageStyle.MeltingHeat,
ImageStyle.MeltingHeat2,
ImageStyle.Negative
};
myCaptcha.ImageStyle = CaptchaRandomization.GetRandomImageStyle(allowedStyles);
More details

Related

Is there an easy way to view matrix elements when debugging a C# WPF application?

I'm using complex matrices in my C# WPF application. I would like to view the matrix elements whilst debugging so as to ascertain correct operation of the application. I have tried entering the matrix name in the Watch list but all I get back is Value = DenseMatrix 3x3 Complex.
I have seen with Console applications that it is a trivial exercise to display the matrix entries in the console.
Looking for the best way to do it in WPF.
Thanks.
You should override the ToString() method and implement this to provide a nice way to visualize your matrix as a string. This will make it appear in the debugger when hovering over the object. You may need to format the values to have a fixed size to make all the columns align well.
If you cannot change the class you can still make an extension method, you will need to call this explicitly in an immediate panel or watch window, but this should at least make it possible to view.
It is also possible to create custom data visualizers, but I have never tried this, since visualizing values as strings has always been sufficient for my needs.
Am struggling to get the ToString method to work. My code is summarised as follows:
public class LineFlex
{
public double SideA
{
get { return sideA; }
set { sideA = value;}
}
internal void Calcs()
{
Complex D44 = new Complex(zD44real, zD44imag);
Complex D45 = new Complex(zA12real, zD45imag);
Complex D54 = new Complex(zA12real, zD54imag);
Complex D55 = new Complex(zD44real, zD44imag);
Complex[,] arrayD = new Complex[2, 2] { { D44, D45 }, { D54, D55 }
};
DenseMatrix MatrixD = DenseMatrix.OfArray(arrayD);
}
public override string ToString()
{
return "MatrixD" + SideA;
}
}
However, if I replace SideA with MatrixD (the one I want to view) in the override ToString() method:
public override string ToString()
{
return "MatrixD" + MatrixD;
}
I get an error 'MatrixD does not exist in the current context'. MatrixD sits in the Calcs method in the same class so I thought the MatrixD is available to ToString() as well?
Thanks.

How set a variable the Properties.Settings.Default?

I have two Properties files that store colors.
Properties.Green.Theme
Properties.PurpleTheme
The names of the parameters in the files are completely the same. I want to implement a theme change by switching between these two files. The simplest thing, in theory, is to assign a variable a value with a visibility level for the entire class. For example, var theme = Properties.GreenTheme.Default. And then use this variable in the methods. because the names are the same everywhere. Sample code in what form I want to do it:
public partial class FrmMain : Form{
TypeVar theme = null;
public FrmMain()
{
InitializeComponent();
if (i = 1)
{
theme = Properties.GreenTheme.Default;
}
if (i = 3)
{
theme = Properties.PurpleTheme.Default;
}
SetBottonColor();
SetPanelColor();
}
private void SetBottonColor()
{
btn.BackColor = theme.button_color;
btn2.BackColor = theme.button_color;
btn3.BackColor = theme.button_color;
}
private void SetPanelColor()
{
panel.BackColor = theme.panel_color;
panel2.BackColor = theme.panel_color;
}
}
that is, in order not to prescribe if in each method, I want to create a variable at the class level and change it when loading the form. And since the names of the parameters in the files are the same, the colors would be taken from the desired file. Locally I can easily declare a variable
var theme = Properties.PurpleTheme.Default;
But I need it at the class level because there are a lot of such methods. object as var unfortunately does not work...
p.s. Sorry for my English

In WPF, how do I change the source of an image programatically? [duplicate]

This question already has answers here:
How to load image to WPF in runtime?
(2 answers)
Closed 1 year ago.
I have a custom control with a default image that I want to change based on which iteration of the control it is. For example, I have one for "F1" and "NumLock" and so on. In the constructor of the control, I have this:
public FixerBox(Dictionary<string,string> deets)
{
InitializeComponent();
btnOff();
this.FixerTitle.Text = deets["Title"];
this.FixerDesc.Text = deets["Description"];
this.FixerTags.Text = deets["Tags"];
this.FixerImg.Source = new BitmapImage(new Uri(deets["Img"], UriKind.Relative));
}
The bitmap stuff was based on another answer and produces this:
Below is the control itself showing that it's correctly getting the title, tags, and description, but the image is bunk (on the left side, that thin grey line is the border that should be around the image).c#
If I was using HTML/CSS, I could right-click the image to see what exactly its properties are, but I don't know how to get that kind of information using WPF. The best I could manage was in the top area is a status window where I've manually printed a "Tostring" output of the first controls image source data. Near as I can tell, it's all correct, but there's no actual image there. Every subsequent control has the same output (one thin line where the image should be).
EDIT Per comments, here is some more of the information. The main XAML file loads up the controls like so in its constructor:
public partial class MainWindow : Window
{
private Fixers fixers = new Fixers();
// This is the custom control consisting mostly of various boxes
private Dictionary<string,FixerBox> fixerBoxes = new Dictionary<string, FixerBox> { };
public MainWindow()
{
InitializeComponent();
var fixNames = fixers.FixerNames();
foreach (string key in fixNames)
{
fixerBoxes[key] = new FixerBox(fixers.GetFix(key));
FixersArea.Children.Add(fixerBoxes[key]);
}
StatusBox.Text += fixerBoxes["F1"].FixerImg.Source.ToString();
}
}
The fixers variable is of class Fixers which consists of the below (abbreviated to show just the F1 function for brevity):
class Fixers
{
private string ClearWS(string str)
{
var first = str.Replace(System.Environment.NewLine, "");
return first.Replace("\t", "");
}
// Loads registry functions
private Regis regStuff = new Regis();
// Loads preferences from the file
private Prefs prefs = new Prefs();
// A timer to make sure the system behaves
private Timer watcher;
// Watcher action toggles
private bool watchNumL = false;
// Translation array from fix shortname to various data about them
private Dictionary<string, Dictionary<string, string>> fixers = new Dictionary<string, Dictionary<string, string>>
{
["F1"] = new Dictionary<string,string> {
["PrefName"] = "KillF1UnhelpfulHelp",
["Img"] = #"/graphics/F1key.png",
["Title"] = #"Diable F1 ""Help"" function",
["Description"] = #"
Have you ever hit the F1 key by accident and had a distracting and unhelpful window or webpage open as a result?
Windows set the F1 key to a generic help function that basically never helps and always gets in the way.
Enable this control to disable that obnoxious design choice. Note that some programs still respond to F1 on their own accord,
but this will stop the default Windows behavior in things like Windows Explorer at least.
",
["Tags"] = "#Keyboard,#Rage"
},
};
public Fixers()
{
// The readability hack above with multi-line strings introduces a bunch of extra whitespace. Let's clear that out
foreach (var fixKey in fixers.Keys)
{
fixers[fixKey]["Description"] = ClearWS(fixers[fixKey]["Description"]);
}
}
public List<string> FixerNames()
{
return fixers.Keys.ToList();
}
public bool IsFixed(string which)
{
// If we're watching, it's fixed
if ("NumL" == which) return watchNumL;
// For anything registry related
return regStuff.IsFixed(which);
}
public Dictionary<string,string> GetFix(string which)
{
return fixers[which];
}
}
if you use binding, you can create in your ViewModel a string, in which is stored the path of your image, then you can easily change programatically its path.
Then in XAML just bind image's source to the string.
In my case I have a list of objects, with the property `ImageName' :
<Image Source="{Binding DataContext.SelectedMacro.ImageName,
RelativeSource={RelativeSource AncestorType=Window}}"/>

Passing Parameters from one XAML page to another

I have a Tile Layout Control with of course Tiles in it which I use to let user navigate through pages, I simply want to pass some parameters like strings or integers between my pages... How to do that?
you can try this approach or just by passing data between classes
http://msmvps.com/blogs/siva/archive/2007/05/11/storing-application-wide-data-the-wpf-way.aspx
Application.Current.Properties["youvalueindex"];
Generally you will have some sort of manager controlling your windows. That manager would handle passing the data between the windows. The windows generally should not know anything about each other.
Simplistic Example
class Manager
{
...
void DoSomething()
{
ViewModelA vma = new ViewModelA();
WindowA wa = new WindowA();
wa.DataContext = vma;
wa.ShowDialog();
ViewModelB vmb = new ViewModelB();
vmb.SharedData = vma.SharedData;
WindowB wb = new WindowB();
wb.DataContext = vmb;
wb.ShowDialog();
}
...
}

Click a custom control and show some variables related to it in another control

in my Win Forms app I create an array of dynamic custom controls inside a loop. These, lets call them 'boxes', are like my basic pieces of information. I also create string arrays in other parts of the code that contain the information of this 'boxes', so that for example string[3] is a variable of box[3] and so does stringa[3], stringb[3], stringc[3]... all the arrays with the same index are related to the box with that index. Hope I make myself clear.
Only 2 of this strings are shown in 2 labels inside each custom control 'box' in the array, but the others are there because I want to make something so that when the user clicks one of these controls the other strings can be shown in another control. Sort of something like "More Information...". All the 'boxes' in the array need to have the same event handler because I create +100.
To put it more into context, each custom control 'box' in the array shows the Symbol and the Price of a stock and I want that when the user clicks on each stock more quote information is shown on another special control which is like a placeholder for "More info".
I am thinking of 2 ways to do it:
If I could "detect" the index of the clicked control (which is the same in the strings related to it), I could just set this to an int j and all I have to do is show all the strings a,b,c... with index j. Unfortunately I cannot find a way to do this, maybe it is not even possible.
The other way I have thought is to create some properties for my custom control which "store" this variables, and in my app instead of assigning strings I would set properties for each control, which I could later retrieve when the control is clicked. I haven't tryed this because I don't know exactly how to do it.
What do you think? Do you know how can I achieve this or do you have a different idea that will work? Please help! Thanks in advance.
It's kind of a broad implementation question since there are countless ways you could implement something like this.
If you are creating two collections, one with the buttons and one with the information, you potentially could just assign each of the buttons 'Tag' properties to point to the corresponding info and assign a generic OnClick event handler that displays the info.. something like:
infoControl.text = ((InfoClass)((Button)Sender.Tag)).pieceOfInformation;
But again there are many ways to do this, and the choice comes down to how you store your information.
For your first method, you could have a property of your custom control that is the index.
public class Box : Control
{
// ...existing code
private int index;
public int Index
{
get
{
return index;
}
set
{
index = value;
}
}
}
OR
For your second method, you could have a property of your custom control that is the additional info string.
public class Box : Control
{
// ...existing code
private string extraInfo;
public string ExtraInfo
{
get
{
return extraInfo;
}
set
{
extraInfo = value;
}
}
}
In either case, you could then access the proper information right in your click handler for the "box".
i don't know about the first way - got to noodle around more, but in the second way you can extended your custom or built-in control: for example:
public class ExtendedLabel: Label
{
public string[] MoreInfo { get; set; }
}
and initialize it
public TestForm()
{
InitializeComponent();
ExtendedLabel label = new ExtendedLabel();
label.MoreInfo = new string[] { "test" };
this.Controls.Add(label);
label.AutoSize = true;
label.Location = new System.Drawing.Point(120, 87);
label.Name = "label1";
label.Size = new System.Drawing.Size(35, 13);
label.TabIndex = 0;
label.Text = label.MoreInfo[0];
}
And later in your event handler you can use the inside information

Categories