Problemas!!!
16/11/2008
0
Olá a todos,eu estou com alguns problemas,não estou conseguindo instanciar minha classe,minha classe está em uma pasta e o aspx está em outra pasta,queria ver com voces se está correto o que eu fiz,por favor,olhem e me digam se está correto,caso não,corrijam:
[b:a80e847501]Classe:[/b:a80e847501]
ImagemCaptcha.aspx
[b:a80e847501]Cadastro.aspx[/b:a80e847501]
O outro problema é com o meu visual studio 2005,quando arrasto um componente html para o web form ou quando eu crio um table ele está gerando no código as tags em UCase e não LCase como era pra ser,pois por exemplo <td> é o certo mas ele gera <TD> e assim o xhtml validation dá erro pois essa tag não existe,dae eu tenho que teclar ctrl + h e dar um replace all mudando pra tag certa,mas quando eu executo o web site pra teste e dou um stop depois de ter testado as tags voltam a ficar maiusculas ,eu fui lá nas opções de validation e formating e o engraçado é que lá em client tags tá como lowercase ,como eu resolvo isso? pois é um saco ficar ter que mudando a cada execução e ficar vendo cento e trinta e poucos erros de html
PS:Estou usando o asp.net ajax framework,isso influencia em algo?
Por favor,eu suplico, me ajudem.
[b:a80e847501]Classe:[/b:a80e847501]
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using Microsoft.CSharp; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Drawing.Text; namespace Captcha { /// <summary> /// Classe Captcha:Gera uma imagem com um caracteres aleatórios para fazer a validação por imagem. /// </summary> public class Captcha { //Propriedades public String Texto { get { return this.m_texto; } } public Bitmap Imagem { get { return this.m_imagem; } } public int Largura { get { return this.m_largura; } } public int Altura { get { return this.m_altura; } } //Implementação das propriedades private string m_texto; private int m_largura; private int m_altura; private string fonte; private Bitmap m_imagem; //Para gerar valores aleatórios private Random Aleatorio = new Random(); //Gera uma nova instância da classe Captcha usando os atributos texto, largura e altura public Captcha(string s, int largura, int altura) { this.m_texto = s; this.ConfiguraTamanho(largura, altura); this.GeraImagem(); } //Gera uma nova instância da classe Captcha usando os atributos texto, largura, altura e fonte public Captcha(string s, int largura, int altura, string fonte) { this.m_texto = s; this.ConfiguraTamanho(largura, altura); this.ConfiguraFonte(fonte); this.GeraImagem(); } ~Captcha() { Dispose(false); } //Libera todos os recursos utilizados por esse objeto public void Dispose() { GC.SuppressFinalize(this); this.Dispose(true); } //Limpa recursos restados protected virtual void Dispose(bool disposing) { if(disposing) { //Limpa a imagem this.m_imagem.Dispose(); } } //Configura a largura e a altura da imagem private void ConfiguraTamanho(int largura, int altura) { //Verifica a largura e altura if(largura <= 0) { throw new ArgumentOutOfRangeException("largura", largura, "Valor incorreto, precisa ser maior que 0 !"); } if(altura <= 0) { throw new ArgumentOutOfRangeException("altura", altura, "Valor incorreto, precisa ser maior que 0 !"); } this.m_largura = largura; this.m_altura = altura; } //Configura a fonte do texto exibido na imagem private void ConfiguraFonte(string fonte) { //Se a fonte escolhida não existir então utiliza uma fonte padrão try { Font font = new Font(this.fonte, 12f); this.fonte = fonte; font.Dispose(); } catch(Exception ex) { this.fonte = System.Drawing.FontFamily.GenericSerif.Name; } } //Cria uma imagem bitmap private void GeraImagem() { //Cria uma nova imagem bitmap de 32 bits Bitmap bitmap = new Bitmap(this.m_largura, this.m_altura, PixelFormat.Format32bppArgb); //Cria uma objeto gráfico para desenho Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle ret = new Rectangle(0, 0, this.m_largura, this.m_altura); //Preenche o fundo da imagem HatchBrush hb = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White); g.FillRectangle(hb, ret); //Configura a fonte do texto SizeF sf = default(SizeF); float fonteTamanho = ret.Height + 1; Font font = default(Font); //Ajusta o tamanho da fonte até que o texto se encaixe com a imgem do { fonteTamanho -= 1; font = new Font(this.fonte, fonteTamanho, FontStyle.Bold); sf = g.MeasureString(this.m_texto, font); } while(sf.Width > ret.Width); //Configura o formato do texto StringFormat formato = new StringFormat(); formato.Alignment = StringAlignment.Center; formato.LineAlignment = StringAlignment.Center; //Cria um caminho usando o texto e posicionando-o aleatoriamente GraphicsPath caminho = new GraphicsPath(); caminho.AddString(this.m_texto, this.fonte, (int)font.Style, font.Size, ret, formato); float v = 4f; PointF[] pf = {new PointF(this.Aleatorio.Next(ret.Width) / v, this.Aleatorio.Next(ret.Height) / v), new PointF(ret.Width - this.Aleatorio.Next(ret.Width) / v, this.Aleatorio.Next(ret.Height) / v), new PointF(this.Aleatorio.Next(ret.Width) / v, ret.Height - this.Aleatorio.Next(ret.Height) / v), new PointF(ret.Width - this.Aleatorio.Next(ret.Width) / v, ret.Height - this.Aleatorio.Next(ret.Height) / v) }; Matrix matrix = new Matrix(); matrix.Translate(0f, 0f); caminho.Warp(pf, ret, matrix, WarpMode.Perspective, 0f); //Desenha o texto hb = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray); g.FillPath(hb, caminho); //Adiciona um efeito aleatório int m = Math.Max(ret.Width, ret.Height); for(int i = 0; i <= (int)(ret.Width * ret.Height / 30f) - 1; i++) { int x = this.Aleatorio.Next(ret.Width); int y = this.Aleatorio.Next(ret.Height); int w = this.Aleatorio.Next(m / 50); int h = this.Aleatorio.Next(m / 50); g.FillEllipse(hb, x, y, w, h); } //Limpa font.Dispose(); hb.Dispose(); g.Dispose(); //Coloca a imagem this.m_imagem = bitmap; } private string GeraCodigoAleatorio() { //Retorna uma string de 7 digitos aleatórios string s = ""; for (int i = 0; 1 <= 6; i++) { s = string.Concat(s, this.Aleatorio.Next(10).ToString()); } retirn s; } private bool VerificaCaptcha() { //Verifica se o que foi digitado equivale ao valor da imagem if (txtCodigoCaptcha.text == Session("CaptchaImageText").ToString()) { //Exibe uma mensagem de aviso lblCaptchaMsg.text = "Código Correto."; return true; } else { //Exibe uma mensagem de erro lblCaptchaMsg.text = "Código Incorreto."; //Limpa o campo e cria um novo codigo aleatorio txtCodigoCaptcha.text = ""; Session("CaptchaImageText") = GeraCodigoAleatorio(); return false; } } } }
ImagemCaptcha.aspx
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using System.Drawing.Imaging; public partial class Web_ImagemCaptcha : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //Cria uma imagem Captcha usando o texto armazenado na sessão Captcha.Captcha c = new Captcha.Captcha(this.Session("CaptchaImageText").ToString(), 200, 50, "E-Shopping"); //Altera a resposta do header para a saida de imagem JPEG this.Response.Clear(); this.Response.ContentType = "image/jpeg"; //Escreve a imagem no stream de resposta no formato jpeg c.Image.Save(this.Response.OutputStream, ImageFormat.Jpeg); //Libera o objeto imagem captcha c.Dispose(); } }
[b:a80e847501]Cadastro.aspx[/b:a80e847501]
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Cadastro : System.Web.UI.Page { //Cria um codigo aleatorio que será armazenado na sessão private Random Aleatorio = new Random(); protected void Page_Load(object sender, EventArgs e) { Captcha.Captcha = new Captcha.Captcha(); if(Page.IsPostBack == false) { this.Session("CaptchaImageText") = Captcha.GeraCodigoAleatorio(); } else { Captcha.VerificaCaptcha(); } } }
O outro problema é com o meu visual studio 2005,quando arrasto um componente html para o web form ou quando eu crio um table ele está gerando no código as tags em UCase e não LCase como era pra ser,pois por exemplo <td> é o certo mas ele gera <TD> e assim o xhtml validation dá erro pois essa tag não existe,dae eu tenho que teclar ctrl + h e dar um replace all mudando pra tag certa,mas quando eu executo o web site pra teste e dou um stop depois de ter testado as tags voltam a ficar maiusculas ,eu fui lá nas opções de validation e formating e o engraçado é que lá em client tags tá como lowercase ,como eu resolvo isso? pois é um saco ficar ter que mudando a cada execução e ficar vendo cento e trinta e poucos erros de html
PS:Estou usando o asp.net ajax framework,isso influencia em algo?
Por favor,eu suplico, me ajudem.
Fagnerx21
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)