IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP.NET Discussion :

ImageHtml et MemoryStream


Sujet :

ASP.NET

  1. #1
    Membre éprouvé
    Avatar de Pelote2012
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2008
    Messages
    925
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Vienne (Limousin)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2008
    Messages : 925
    Billets dans le blog
    2
    Par défaut ImageHtml et MemoryStream
    Dans le cadre de la mise en place d'un control pour faire un Captcha, j'ai crée
    CaptchaControl.vb:
    Code vb : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
     
    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.IO
     
    Imports System.Drawing
    Imports Microsoft.VisualBasic
    Imports System.Drawing.Drawing2D
     
    <DefaultProperty("MyCode"), ToolboxData("<{0}:CaptchaControl runat=server></{0}:CaptchaControl>")> _
    Public Class CaptchaControl
        Inherits WebControl
     
        Dim _MyCode As String = Nothing
     
        <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property MyCode() As String
            Get
                If _MyCode = Nothing Then _MyCode = "Captach@"
                Return _MyCode
            End Get
     
            Set(ByVal Value As String)
                _MyCode = Value
            End Set
        End Property
     
        Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
     
            Dim MyImg As New Web.UI.HtmlControls.HtmlImage
            MyImg.Attributes.Add("TITLE", _MyCode)
            'MyImg.Attributes.Add("SRC", BuildMyImg)
            MyImg.RenderControl(output)
     
        End Sub
     
        Protected Function BuildMyImg() As System.IO.MemoryStream
     
            Dim memSt As New System.IO.MemoryStream
     
            Dim b As New Bitmap(150, 30, Imaging.PixelFormat.Format32bppArgb)
            Dim gr As Graphics = Graphics.FromImage(b)
            Dim str As String = MyCode
     
            Dim sizeff As SizeF = gr.MeasureString(str, New Font("Arial", 25, FontStyle.Strikeout, GraphicsUnit.Pixel), 300)
            Dim hatchBrush As HatchBrush = New HatchBrush(HatchStyle.Wave, Color.DarkBlue, Color.Gray)
            gr.FillRectangle(hatchBrush, New Rectangle(0, 0, 150, 30))
            gr.DrawString(str, New Font("Arial", 15, FontStyle.Strikeout, GraphicsUnit.Point), Brushes.White, 5, 5)
            b.Save(memSt, Drawing.Imaging.ImageFormat.Jpeg)
            b.Dispose()
     
            Return memSt
     
        End Function
    End Class

    Mon problème est que je n'arrive pas à lier l'image bitmap de ma fonction BuildMyImg avec MyImg.Attributes.Add("SRC", BuildMyImg)
    de RenderContents, j'ai essayé par aussi par tableau de byte ... et toujours sans succès.
    J'ai regarder sur les forum et google (D'ailleur la constitution de l'image est reprise d'un script en httphandler de développez.com).

    Je précise que le but de ce control est d'être extérieur à ma solution.
    Je lui génère une chaine aléatoirement et il doit former le captcha à partir de cette chaine. Dans ma solution, je n'ai plus qu'à comparer ce que la personne à mis avec CaptchaControl.MyCode.

    Je précise aussi que l'attribut "title" marche très bien. Quand je passe ma souris sur l'emplacement de l'image je vois bien s'afficher dans un infobulle la string générée. Par contre, à chaque essai, j'avais une croix rouge au lieu de mon image.

    Quelqu'un peut-il m'aider?

  2. #2
    Membre éprouvé
    Avatar de Pelote2012
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2008
    Messages
    925
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Vienne (Limousin)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2008
    Messages : 925
    Billets dans le blog
    2
    Par défaut
    Je suis aussi ouvert à tout autre proposition me permettant d'atteindre mon objectif : avoir un control qui affiche le code et qui me retourne le code pour vérification au niveau serveur.

    J'ai essayé sans succès avec le httphandler.
    Mais bon, je suis débutant en asp.net

  3. #3
    Membre chevronné Avatar de predalpha
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    392
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 392
    Par défaut
    je ne m'y connais pas trop en httphandler et autres mais peut être peux tu jeter un oeil aux solutions existantes soit pour t'en inspirer, soit pour les utiliser directement (pourquoi réinventer la roue ?)

    http://www.mondor.org/captcha.aspx
    http://www.codeproject.com/KB/custom...tchaNET_2.aspx
    http://www.codeproject.com/KB/custom...haControl.aspx

  4. #4
    Membre éprouvé
    Avatar de Pelote2012
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2008
    Messages
    925
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Vienne (Limousin)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2008
    Messages : 925
    Billets dans le blog
    2
    Par défaut
    Merci pour ta réponse.

    Ta solution m'irait très bien sans se foutu pb que je n'y comprend rien.

    En reprenant le 1er lien, j'applique dans un projet test : pas de problème.
    Je refais la même chose dans mon projet : est là croix rouge au lieu de voir mon captcha.

    J'ai eu le même pb avec la solution par httpHandler. ça marche dans le même projet test mais pas dans mon projet final...

    Je ne voit pas d'où ça peut venir...

    Quelqu'un a certainement déjà eu affaire à cela. Pouvez vous me mettre sur une piste.

    Là je craque. ça se trouve c'est une bêtise grosse comme une maison

    Par contre si quelqu'un peut répondre à la prelière question, ça me va aussi

  5. #5
    Membre éprouvé
    Avatar de Pelote2012
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2008
    Messages
    925
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Vienne (Limousin)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2008
    Messages : 925
    Billets dans le blog
    2
    Par défaut ...
    Je ne trouve toujours pas
    Le pire c'est que quand on a déployé en prod ça a marché pendant 3 semaines puis même problème qu'en Dev

    Je sèche sur le pb.

  6. #6
    Membre éprouvé
    Avatar de Pelote2012
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2008
    Messages
    925
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Vienne (Limousin)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2008
    Messages : 925
    Billets dans le blog
    2
    Par défaut Je progesse mais ...
    CaptchaControl.vb:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
     
    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.IO
     
    Imports System.Drawing
    Imports Microsoft.VisualBasic
    Imports System.Drawing.Drawing2D
     
    <DefaultProperty("MyCode"), ToolboxData("<{0}:CaptchaControl runat=server></{0}:CaptchaControl>")> _
    Public Class CaptchaControl
        Inherits WebControl
     
        Dim _MyCode As String = Nothing
     
        <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property MyCode() As String
            Get
                If _MyCode = Nothing Then _MyCode = "Captach@"
                Return _MyCode
            End Get
     
            Set(ByVal Value As String)
                _MyCode = Value
            End Set
        End Property
     
        Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
     
            'Dim MyImg As New System.Web.UI.HtmlControls.HtmlImage 'Image
     
            'MyImg = 'System.Drawing.Image.FromStream(BuildMyImg).Clone
            'MyImg.Attributes.Add("TITLE", _MyCode)
            Dim MonImg As Object = System.Drawing.Image.FromStream(BuildMyImg).Clone
            MonImg.RenderControl(output)
     
        End Sub
     
        Protected Function BuildMyImg() As System.IO.MemoryStream
     
            Dim memSt As New System.IO.MemoryStream
     
            Dim b As New Bitmap(150, 30, Imaging.PixelFormat.Format32bppArgb)
            Dim gr As Graphics = Graphics.FromImage(b)
            Dim str As String = MyCode
     
            Dim sizeff As SizeF = gr.MeasureString(str, New Font("Arial", 25, FontStyle.Strikeout, GraphicsUnit.Pixel), 300)
            Dim hatchBrush As HatchBrush = New HatchBrush(HatchStyle.Wave, Color.DarkBlue, Color.Gray)
            gr.FillRectangle(hatchBrush, New Rectangle(0, 0, 150, 30))
            gr.DrawString(str, New Font("Arial", 15, FontStyle.Strikeout, GraphicsUnit.Point), Brushes.White, 5, 5)
            b.Save(memSt, Drawing.Imaging.ImageFormat.Png)
            b.Dispose()
     
            Return memSt
     
        End Function
    End Class
    default.aspx.vb
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Imports System
     
    Public Class _Default
        Inherits System.Web.UI.Page
     
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     
            Dim MonImg As New CaptchaControl
            MonImg.MyCode = "blabla"
            PH.Controls.Add(MonImg)
        End Sub
     
    End Class
    Dans ma page default j'ai un placeholder auquel j'ajoute mon control qui devrait être une image

    Le problème est que je n'arrive pas à relier bitmap à une htmlImage : impossible de faire un cast ...
    et quand je fais System.Drawing.Image.FromStream(BuildMyImg).rendercontrol
    ça me répond que rendercontrol n'est pas membre de la classe bitmap.

    BuildMyImg me génère mon image est la met en stream format png, jpeg ...
    mais je n'arrive pas à rattaché ce stream à un htmlImage...

    Je croix que je tourne en rond.

    Je précise que le httphandler ne marche pas sue mon projet final

  7. #7
    Membre éprouvé
    Avatar de Pelote2012
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2008
    Messages
    925
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Vienne (Limousin)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2008
    Messages : 925
    Billets dans le blog
    2
    Par défaut désespoir
    Toujours personne ...
    Je vais finir par croire que je suis le seul au monde à avoir eu ce problème.
    Des fois c'est dur d'être unique
    J'espère que quelqu'un qui lira ce poste d'un membre désespéré pourra m'aider

  8. #8
    Membre éprouvé
    Avatar de Pelote2012
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2008
    Messages
    925
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Vienne (Limousin)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2008
    Messages : 925
    Billets dans le blog
    2
    Par défaut Big Boss
    j'ai trouvé ...

    j'ai fait un fichier captcha.ashx
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
    <%@ WebHandler Language="VB" Class="captcha" %>
     
    Imports System
    Imports System.Web
     
    Imports System.Drawing
    Imports Microsoft.VisualBasic
    Imports System.Drawing.Drawing2D
     
    Public Class captcha : Implements IHttpHandler
     
        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            Dim memSt As New System.IO.MemoryStream
     
            Dim b As New Bitmap(120, 30, Imaging.PixelFormat.Format32bppArgb)
            Dim gr As Graphics = Graphics.FromImage(b)
            Dim str As String = context.Request.QueryString(0)
     
            Dim sizeff As SizeF = gr.MeasureString(str, New Font("Arial", 25, FontStyle.Strikeout, GraphicsUnit.Pixel), 300)
            Dim hatchBrush As HatchBrush = New HatchBrush(HatchStyle.Wave, Color.DarkBlue, Color.Gray)
            gr.FillRectangle(hatchBrush, New Rectangle(0, 0, 120, 30))
            gr.DrawString(str, New Font("Arial", 15, FontStyle.Strikeout, GraphicsUnit.Point), Brushes.White, 5, 5)
     
            context.Response.ClearContent()
            context.Response.ContentType = "image/jpeg"
     
            b.Save(context.Response.OutputStream, Drawing.Imaging.ImageFormat.Jpeg)
            b.Dispose()
        End Sub
     
        Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property
     
    End Class
    et dans la page aspx
    <asp:Image ID="Secur" runat="server"/>

    dans page aspx.vb à l'init
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Secur.ImageUrl = "captcha.ashx?x=fdgdfgfg"
    Vous comprenez qu'il s'agit just de passer le mot à coder comme vous voulez en le chiffrant ou alors passez le paramètre par le cache

    Par rapport à mon problème précédent, ce qui me sauve c'est :
    context.Response.OutputStream
    qui me permet de me passer du cast image en htmlImage :-)

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [C#] Error enregistrer un bitmap dans un memorystream
    Par Ezekhiel dans le forum Windows Mobile
    Réponses: 2
    Dernier message: 15/05/2007, 13h21
  2. Réponses: 4
    Dernier message: 12/12/2006, 10h22
  3. clientdataset et memorystream
    Par edam dans le forum Delphi
    Réponses: 7
    Dernier message: 10/10/2006, 00h59
  4. [DELPHI] Problème de String <-> MemoryStream
    Par Squall78 dans le forum Delphi
    Réponses: 19
    Dernier message: 17/09/2006, 09h56
  5. [VB.Net] Comment ecrire dans un MemoryStream
    Par vijeo dans le forum Windows Forms
    Réponses: 8
    Dernier message: 28/04/2006, 14h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo