Hello,
All. I have run into an issue that has me entirely just stumped. The code gives the following error when "Resizing" images to the Network Share.
A generic error occurred in GDI+
The mail file will be renamed and uploaded to the BIG folder in the network share so the permissions allow it to upload to the folder. However, when the "Resizing" is performed, there is where the issue occurs. I read that it could be caused by the image files being locked, but I am unsure where or what to do about the file being locked. Specs Windows 2016 Server Core
IIS 10 (The network share server is a Windows 2016 Server Core used solely as a network share for hosting all media-related files for the website domains.) Domain Username: imagepool Domain user has the following roles.
Administrators
DnsAdmins
Domain Admins
Domain Computers
Domain Controllers
Domain Users
Enterprise Admins
IIS_Users
Schema Admins
Server Operators
IIS Settings for the domain. App Pool (Integrated) uses the Domain User above as its Identity. Virtual Directory in the site (Pointing to the Network Share) Physical Path: \\ShareName\Images Physical Path Credentials: company-domain-name\imagepool ======= Provided is the code for uploading images and resizing them.
In this code example, I have it set to run in the following ways: Visual Studio - Saves files to the directory. (Even within VS, the file will not upload to the Network Share) IIS - Saves files to the network share.
Load.aspx.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Load.aspx.vb" Inherits="Load" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(function () {
$("[id*=fuUpload]").change(function () {
if (typeof (FileReader) != "undefined") {
var dvPreview = $("#dvPreview");
dvPreview.html("");
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
$($(this)[0].files).each(function () {
var file = $(this);
const size = (file[0].size / 1000 / 1000).toFixed(2);
const theSize = size + (size > 1 ? "mb" : "kb");
var maxSize = 1 // Change this to the max allowed to upload. 1 = 1mb / 5 = 5mb / 10 = 10mb / 20 = 20mb
if (regex.test(file[0].name.toLowerCase())) {
var reader = new FileReader();
reader.onload = function (e) {
var strImage = e.target.result
if (size > maxSize) {
dvPreview.append("File size is larger than the allowed size. (Fize Size =" + theSize + ") <br />Please upload less than " + maxSize + "mb per image.<br />Upload button has been diabled. <br />Please change file size to meet the requirements, <br />Refresh and Re-Upload files.")
$("#btnUpload").prop('disabled', true);
} else {
dvPreview.append('<div class="row">' +
'<div class="Sizes column">' + theSize + '</div>' +
'<div class="ImageList column"><img src="' + strImage + '" style="height:100px;"></div>' +
'<div class="theTitle column"><span class="Info">Title:</span><br /><input type="text" name="ImageTitle"/></div>' +
'</div > ');
}
}
reader.readAsDataURL(file[0]);
} else {
dvPreview.html(file[0].name + " is not a valid image file.");
return false;
}
});
} else {
alert("This browser does not support HTML5 FileReader.");
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">1 mb file upload only!<br />
<asp:FileUpload ID="fuUpload" runat="server" multiple="multiple" />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="Upload" /><br />
<asp:Label ID="IDLabel" runat="server" Text="Label"></asp:Label>
<hr />
<div id="dvPreview">
</div>
</form>
</body>
</html>
Load.aspx.vb
Imports System
Imports System.Web
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Partial Class Load
Inherits Page
Public Class GlobalVariables
Public Shared MovePath As String
Public Shared theLocal As String = HttpContext.Current.Request.ServerVariables("SERVER_NAME")
End Class
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim strid As String = Request.QueryString("ID")
If strid <> "" Then
IDLabel.Text = "Page set to Upload!"
fuUpload.Enabled = True
btnUpload.Enabled = True
Else
IDLabel.Text = "<a href='?ID=1'>Load ID into Query</a> to enable upload."
fuUpload.Enabled = False
btnUpload.Enabled = False
End If
End Sub
Protected Sub Upload(sender As Object, e As EventArgs)
Dim cn As New SqlConnection
Dim objConnection As OleDbConnection = Nothing
Dim objCmd As SqlCommand = Nothing
Dim strSQL As [String]
cn = New SqlConnection(ConfigurationManager.ConnectionStrings("Virtual-Learning").ConnectionString)
cn.Open()
If GlobalVariables.theLocal = "localhost" Then
GlobalVariables.MovePath = Context.Server.MapPath("ImageServer/")
Else
GlobalVariables.MovePath = ("\\ShareName\\Images\\")
End If
Dim strid As String = Request.QueryString("ID")
Dim getun As String = "experts-exchange" 'this will come from a cookie with the real users username
Try
For i As Integer = 0 To Request.Files.Count - 1
Dim postedFile As HttpPostedFile = Request.Files(i)
If postedFile.ContentLength > 0 Then
'=-=-=-=-=-=-=-=-Code starts here-=-=-=-=-=-=-=-=
Dim bytes As Byte() = New Byte(postedFile.InputStream.Length - 1) {}
Dim getName As String() = Split(Request.Form("ImageTitle"), ",")
Dim ext As String = System.IO.Path.GetExtension(postedFile.FileName)
Dim Gallery As String = GlobalVariables.MovePath & getun & "/"
Dim thPath As String = ""
Dim thImageFolder As String = ""
Dim theImageFolder As String = ""
Dim savethPath As String = ""
Dim tempPath As String = ""
Dim savepath As String = ""
Dim now As DateTime = DateTime.Now
Dim getYear As String = (((((now.ToString("hh") & "") + now.ToString("mm") & "") + now.ToString("ss") & "") + now.ToString("mm") & "") + now.ToString("dd") & "") + now.ToString("yyyy") + now.ToString("fff") + ""
tempPath = Gallery & ConfigurationManager.AppSettings("BigFolderPath") ' Big Folder
thPath = Gallery & ConfigurationManager.AppSettings("thPath") ' Thumb Folder
thImageFolder = Gallery & ConfigurationManager.AppSettings("ImageFolderPath")
Dim changeSize As String = GlobalVariables.MovePath & getun & "\" & ConfigurationManager.AppSettings("thPath")
theImageFolder = (thImageFolder) ' ImageServer/ Folder
'savethPath = context.Server.MapPath(thPath) ' + getun
savethPath = ConfigurationManager.AppSettings("ImageFolderPath") + "\" + getun + "\" + ConfigurationManager.AppSettings("thPath") + "\" '"ImageServer\carrzkiss\Thumbs\"
savepath = (tempPath)
Dim getFile As String = getYear + ext '+ clear 'And + clear
Dim subTpath As String = Path.Combine(theImageFolder, tempPath)
If Not Directory.Exists(subTpath) Then
Directory.CreateDirectory(subTpath)
End If
Dim subPath As String = Path.Combine(theImageFolder, thPath)
If Not Directory.Exists(subPath) Then
Directory.CreateDirectory(subPath)
End If
' Get the Height and Width of the image and send it to the database.
Dim GetImageWH As Image
GetImageWH = Image.FromStream(postedFile.InputStream)
Dim PicW As String = GetImageWH.Height
Dim PicH As String = GetImageWH.Width
Dim PicWH As String = PicH + "x" + PicW
postedFile.SaveAs((savepath & "\") + getFile)
Response.Write((savethPath & "\") + getFile & "<hr />") ' Show the directory and file being uploaded.
Dim ImageToSave As Image = resizeImage(Image.FromStream(postedFile.InputStream), New Size(130, 110))
ImageToSave.Save(Context.Server.MapPath(Path.Combine(savethPath, getFile.Insert(getFile.LastIndexOf("."c), ""))), ImageFormat.Jpeg)
Context.Response.StatusCode = 200
strSQL = "INSERT INTO ImageServer(PicsPath, PicsSize, PicWH, UserID, PicsName)VALUES(@PicsPath, @PicsSize, @PicWH, @UserID, @PicsName)"
objCmd = New SqlCommand(strSQL, cn)
objCmd.Parameters.Add(New SqlParameter("@PicsPath", getFile))
objCmd.Parameters.Add(New SqlParameter("@PicsSize", bytes.Length))
objCmd.Parameters.Add(New SqlParameter("@PicWH", PicWH))
objCmd.Parameters.Add(New SqlParameter("@Userid", strid))
objCmd.Parameters.Add(New SqlParameter("@PicsName", getName(i)))
objCmd.ExecuteNonQuery()
Response.Write(getFile & "(" & getName(i) & ") Uploaded Successfully!<br />")
'=-=-=-=-=-=-=-=-Code ends here=--=-=-=-=-=-=-=-=
End If ' postedFile.ContentLength
Next
Catch ex As Exception
Dim st As StackTrace = New StackTrace(ex, True)
Response.Write("Seems there was an issue, please try uploading your file again." + (ex.Message + TimeString) + "<br />")
Response.Write(st)
Finally
cn.Close()
End Try
End Sub
Private Shared Function resizeImage(ByVal imgToResize As Image, ByVal size As Size) As Image
Dim sourceWidth As Integer = imgToResize.Width
Dim sourceHeight As Integer = imgToResize.Height
Dim nPercent As Single = 0
Dim nPercentW As Single = 0
Dim nPercentH As Single = 0
nPercentW = (CSng(size.Width) / CSng(sourceWidth))
nPercentH = (CSng(size.Height) / CSng(sourceHeight))
If nPercentH < nPercentW Then
nPercent = nPercentH
Else
nPercent = nPercentW
End If
Dim destWidth As Integer = CInt((sourceWidth * nPercent))
Dim destHeight As Integer = CInt((sourceHeight * nPercent))
Dim b As New Bitmap(destWidth, destHeight)
Dim g As Graphics = Graphics.FromImage(DirectCast(b, Image))
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight)
g.Dispose()
Return DirectCast(b, Image)
End Function
End Class
Thanks for any help on this one. Wayne