Thread was being aborted

Asked By Andy Kalbvleesch
15-Feb-08 01:07 PM
Earn up to 10 extra points for answering this tough question.

Hi all,

I have a problem and I think what causes the problem. When running the page below I get the error Thread was being aborted . Probably because the routines are not finished before the next routine starts so buffer gets overloaded or something. Anybody a clue how t solve this ?

Imports System.Drawing
Imports System.Drawing.Image
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Net.Mail

Partial Class beheer_Zoomyficator
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myDirInfo As DirectoryInfo = New DirectoryInfo(Server.MapPath("~\beheer\productFotoUpload"))
        Dim fotoArray() As DirectoryInfo = myDirInfo.GetDirectories()
        Dim i As Integer = 0
        Dim conn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("freshcottonConnectionString").ToString)
        conn.Open()
        While i < fotoArray.Length
            Dim Sql As String = "SELECT product_id FROM tblProduct WHERE product_id = '" & fotoArray(i).ToString & "'"
            Dim objDR As SqlDataReader
            Dim Cmd As New SqlCommand(Sql, conn)
            Dim product_id As String = fotoArray(i).ToString
            objDR = Cmd.ExecuteReader()
            If objDR.HasRows = True Then
                opslaan(product_id)
            Else
                Directory.Move(Server.MapPath("~\beheer\productFotoUpload\" & product_id), Server.MapPath("~\beheer\productFotoReject\" & product_id))
            End If
            objDR.Close()
            i += 1
        End While
        conn.Close()
    End Sub

    Sub opslaan(ByVal product_id As String)
        If Directory.Exists(Server.MapPath("~\productfotos\" & product_id)) Then
            Directory.Delete(Server.MapPath("~\productfotos\" & product_id), True)
            'Directory.Move(Server.MapPath("~\beheer\productFotoUpload\" & product_id), Server.MapPath("~\beheer\productFotoReject\" & product_id))
        End If
        Directory.CreateDirectory(Server.MapPath("~\productfotos\" & product_id))
        Dim foto_id As Integer = 1
        While foto_id < 5
            Dim originalFile As String = Server.MapPath("~\beheer\productFotoUpload\" & product_id & "\" & foto_id.ToString & ".jpg")
            If File.Exists(originalFile) Then
                Dim targetFolder As String = Server.MapPath("~\productfotos\" & product_id & "\" & foto_id.ToString)
                Directory.CreateDirectory(targetFolder)
                resize(362, 482, originalFile, targetFolder & "\large.jpg")
                resize(144, 192, originalFile, targetFolder & "\medium.jpg")
                resize(78, 104, originalFile, targetFolder & "\small.jpg")
                resize(33, 44, originalFile, targetFolder & "\thumb.jpg")
                zoomify(originalFile, targetFolder)
                File.Delete(originalFile)
            End If
            foto_id += 1
        End While
        Directory.Delete(Server.MapPath("~\beheer\productFotoUpload\" & product_id), True)
        Response.Write("Map " & product_id & " toegevoegd.<br />")
    End Sub

    Sub resize(ByVal width As Integer, ByVal height As Integer, ByVal originalFile As String, ByVal targetFile As String)
        'Dim g As Image = FromFile(originalFile)
        'Dim imgFormat As System.Drawing.Imaging.ImageFormat = Imaging.ImageFormat.Jpeg
        'Dim imgOutput As New Bitmap(g, width, height)
        'imgOutput.Save(targetFile, imgFormat)
        'g.Dispose()

        Dim img As System.Drawing.Bitmap = System.Drawing.Bitmap.FromFile(originalFile)
        Dim imageFormat = img.RawFormat
        'Dim imageFormat = Imaging.ImageFormat.Jpeg
        Dim imgOutput As New Bitmap(img, width, height)
        Dim myresizer As Graphics
        myresizer = Graphics.FromImage(imgOutput)
        myresizer.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        myresizer.DrawImage(img, 0, 0, width, height)
        imgOutput.Save(targetFile, imageFormat)
        img.Dispose()
        imgOutput.Dispose()
        myresizer.Dispose()
    End Sub

    Sub zoomify(ByVal originalFile As String, ByVal targetFolder As String)
        Directory.CreateDirectory(targetFolder & "\zoomifyer\TileGroup0")
        sizeAndTile(4, 1800, 2400, 256, 256, originalFile, targetFolder & "\zoomifyer\TileGroup0\")
        sizeAndTile(3, 900, 1200, 256, 256, originalFile, targetFolder & "\zoomifyer\TileGroup0\")
        sizeAndTile(2, 450, 600, 256, 256, originalFile, targetFolder & "\zoomifyer\TileGroup0\")
        sizeAndTile(1, 225, 300, 256, 256, originalFile, targetFolder & "\zoomifyer\TileGroup0\")
        sizeAndTile(0, 112, 150, 256, 256, originalFile, targetFolder & "\zoomifyer\TileGroup0\")
        Dim fp As StreamWriter
        fp = File.CreateText(targetFolder & "\zoomifyer\ImageProperties.xml")
        fp.WriteLine("<IMAGE_PROPERTIES WIDTH=""1800"" HEIGHT=""2400"" NUMTILES=""109"" NUMIMAGES=""1"" VERSION=""1.8"" TILESIZE=""256"" />")
        fp.Close()
    End Sub

    Sub sizeAndTile(ByVal niveau As Integer, ByVal width As Integer, ByVal height As Integer, ByVal tileWidth As Integer, ByVal tileHeight As Integer, ByVal sourceFile As String, ByVal targetFolder As String)
        Dim g As Image = FromFile(sourceFile)
        Dim imgOutput As New Bitmap(g, width, height)
        g.Dispose()
        Dim resizedFile As String = targetFolder & "temp" + niveau.ToString & ".jpg"
        imgOutput.Save(resizedFile)
        Dim kolom As Integer = 0
        Dim rij As Integer = 0
        While (kolom * tileWidth) < width
            Dim thisWidth As Integer
            If (width - (kolom * tileWidth)) < tileWidth Then
                thisWidth = (width - (kolom * tileWidth))
            Else
                thisWidth = tileWidth
            End If
            While (rij * tileHeight) < height
                Dim thisheight As Integer
                If (height - (rij * tileHeight)) < tileHeight Then
                    thisheight = (height - (rij * tileHeight))
                Else
                    thisheight = tileHeight
                End If
                tile((kolom * tileWidth), (rij * tileHeight), thisWidth, thisheight, resizedFile, targetFolder & niveau.ToString & "-" & kolom.ToString & "-" & rij.ToString & ".jpg")
                rij += 1
            End While
            rij = 0
            kolom += 1
        End While
        File.Delete(resizedFile)
    End Sub

    Sub tile(ByVal left As Integer, ByVal top As Integer, ByVal width As Integer, ByVal height As Integer, ByVal sourceFile As String, ByVal targetFile As String)
        'Dim OriginalImage As Image = FromFile(sourceFile)
        'Dim imgFormat As System.Drawing.Imaging.ImageFormat = Imaging.ImageFormat.Jpeg
        'Dim btmCropped As New Bitmap(width, height)
        'Dim grpOriginal As Graphics = Graphics.FromImage(btmCropped)
        'grpOriginal.DrawImage(OriginalImage, New Rectangle(0, 0, btmCropped.Width, btmCropped.Height), left, top, btmCropped.Width, btmCropped.Height, GraphicsUnit.Pixel)
        'btmCropped.Save(targetFile, imgFormat)
        'OriginalImage.Dispose()
        'btmCropped.Dispose()
        'grpOriginal.Dispose()
        Dim img As System.Drawing.Bitmap = System.Drawing.Bitmap.FromFile(sourceFile)
        'Dim imageFormat = img.RawFormat
        Dim imageFormat = Imaging.ImageFormat.Jpeg
        Dim imgOutput As New Bitmap(img, width, height)
        Dim myresizer As Graphics
        myresizer = Graphics.FromImage(imgOutput)
        myresizer.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        'myresizer.DrawImage(img, 0, 0, width, height)
        myresizer.DrawImage(img, New Rectangle(0, 0, imgOutput.Width, imgOutput.Height), left, top, imgOutput.Width, imgOutput.Height, GraphicsUnit.Pixel)
        imgOutput.Save(targetFile, imageFormat)
        img.Dispose()
        imgOutput.Dispose()
        myresizer.Dispose()
    End Sub

End Class

Create New Account